Here why cin >> cin is not prompting for command line input. Visual Studio gives warning as C4552 '>>': result of expression not used.
int main()
{
int cin = 10;
cin >> cin;
cout << "print" << " " << cin << endl;
}
Related
I'm having some trouble on my code if yourcurrency is larger than convert the convert will choose the 2 instead of 1
can't figure it out what's wrong with my code i've tried on different compiler I think the issue is the code itself can you help me guys?
#include <iostream>
#include <string>
using namespace std;
int picked[5] = {0,0,0,0,0};
int main(){
string currentvalue[5][5] = {{"1","0.01818484","0.015423716","2.4462308","0.019060361"},{"55.012111","1","0.84815483","134.47933","1.0482778"},{"64.90818","1.1789799","1","158.58285","1.2359668"},{"0.40901121","0.0074303636","0.0063022858","1","0.0077907482"},{"52.511804","0.95391572","0.80907737","128.35738","1"}};
string currency[5] = {"Philippine Peso","euro","pounds","yen","usd"};
int yourcurrency;
int convert;
int timesToRun = 5;
int number = 1;
system("COLOR 0a");
cout << "Choose your currency \n" << endl;
for (int counter = 0 ; counter < timesToRun; counter++)
{
cout << number;
cout << "." + currency[counter] << endl;
number++;
}
cout << "\nOption: ";
cin >> yourcurrency;
system("CLS");
yourcurrency = yourcurrency - 1;
picked[yourcurrency] = 1;
cout << "Select your currency you want to convert into \n" << endl;
number = 1;
for (int counter = 0; counter < timesToRun; counter++)
{
if (picked[counter] != 1){
cout << number;
cout << "." + currency[counter] << endl;
number++;
}
}
cout << "\nOption: ";
cin >> convert;
system("CLS");
cout << currency[yourcurrency]+ " - " + currency[convert];
cout << " [" + currentvalue[yourcurrency][convert] + "] " << endl;
cout << "Amount: ";
int cash;
cin >> cash;
double value = stof(currentvalue[yourcurrency][convert]);
double total = cash * value;
cout << currency[convert]<< ": " << total;
}
I want to be able to comment only the first occurrence of cin >> n; after int main.
Doing this through bash, I've tried a combination of using grep and sed, but I'm new to bash and I'm not sure how to accomplish this.
int main(int argc, char** argv)
{
int n;
cin >> n;
cout << factorial(n) << endl;
if (n > 10)
{
cin >> n; // don't want it to change this one!
double d = log(n);
cout << d;
}
return 0;
}
You can do this with sed and a range-expression 0,/regex/s/regex/replacement/ which has the effect of putting sed at the end of it range when the replacement is made limiting the replacement to the first occurrence, e.g.
sed '0,/cin >> n/s/cin >> n/\/\/ cin >> n/' file
Result:
int main(int argc, char** argv)
{
int n;
// cin >> n;
cout << factorial(n) << endl;
if (n > 10)
{
cin >> n; // don't want it to change this one!
double d = log(n);
cout << d;
}
return 0;
}
If you have code before main that can include cin >> n, then you can use int main as the start of the range, e.g.
sed '/int main/,/cin >> n/s/cin >> n/\/\/ cin >> n/' file
Thanks to #BenjaminW
If you don't like the \/\/ look, you can choose an alternative delimiter that will cut down on the picket-fence look, e.g.
sed '/int main/,/cin >> n/s|cin >> n|// cin >> n|' file
$ awk '/int main/{f=1} f && sub(/cin >> n/,"// &"){f=0} 1' file
int main(int argc, char** argv)
{
int n;
// cin >> n;
cout << factorial(n) << endl;
if (n > 10)
{
cin >> n; // don't want it to change this one!
double d = log(n);
cout << d;
}
return 0;
}
#include <iostream>
int main()
{
using namespace std;
int P, C, M, marks;
cout << "Grading System\n\n";
cout << "Enter Marks in Phy\n";
cin >> P;
cout << "Enter Marks in Chem\n";
cin >> C;
cout << "Enter Marks in Maths\n";
cin >> M;
cout << "Grades\n\nPhysics Chemistry Maths\n";
if (marks < 50)
cout << "Fail";
else if (marks <= 60)
cout << "C";
else if (marks <= 70)
cout << "B";
else if (marks <= 80)
cout << "B+";
else if (marks <= 90)
cout << "A";
else if (marks <= 95)
cout << "A+";
else
cout << "Error";
return 0;
}
Is there any way to run above if statement for multiple values of marks like
marks = P
then test for marks = C, marks = M and give output respectively,
You could make your program more modular by implementing the test marks logic in a separate function and name it something like checkGrade and pass as an argument the values you took as input.
For example:
#include <iostream>
using namespace std; //I moved this command outside the main function so it applies globally
void checkGrade(int);
int main()
{
int P, C, M;
cout << "Grading System\n\n";
cout << "Enter Marks in Phy\n";
cin >> P;
cout << "Enter Marks in Chem\n";
cin >> C;
cout << "Enter Marks in Maths\n";
cin >> M;
cout << "Grades\n\nPhysics Chemistry Maths\n";
checkGrade(P);
checkGrade(C);
checkGrade(M);
return 0;
}
void checkGrade(int marks){
if (marks < 50)
cout << "Fail";
else if (marks <= 60)
cout << "C";
else if (marks <= 70)
cout << "B";
else if (marks <= 80)
cout << "B+";
else if (marks <= 90)
cout << "A";
else if (marks <= 95)
cout << "A+";
else{cout << "Error";}
}
Or you could use an array like MadaZZ suggested to eliminate the need to call checkGrade() separately for every user input.
Make an array of marks and a function to check respective grades.
#include <iostream>
using namespace std;
void checkGrade(int marks) //function to check grade
{
if (marks < 50)
cout << "Fail";
else if (marks <= 60)
cout << "C";
else if (marks <= 70)
cout << "B";
else if (marks <= 80){
cout << "B+";
}
else if (marks <= 90){
cout << "A";
}
else if (marks <= 95){
cout << "A+";
}
else{
cout << "Error";
}
}
int main()
{
cout << "Enter no. of students\n\n";
int students;
cin >> students;// Take input of no. of students
int P[students], C[students], M[students]; //Declare array of marks, with each index depicting each student. Size of the array would be the number of students you entered
for( int i = 0; i < students; i++ )
{
cout << "Grading System\n\n";
cout << "Enter Marks in Phy\n";
cin >> P[i];
cout << "Enter Marks in Chem\n";
cin >> C[i];
cout << "Enter Marks in Maths\n";
cin >> M[i];
cout << "Grades\n\nPhysics Chemistry Maths\n";
int count = 3;
while(count) //To check grades in different subjects, the loop runs 'count' number of times ie. for each subject.
{
if(count == 3){
cout<<"\n Grade in Physics ";
checkGrade(P[i]); //Function called to check grade
}
else if(count == 2){
cout<<"\n Grade in Chemistry ";
checkGrade(C[i]); //Function called to check grade
}
else if(count == 1){
cout<<"\n Grade in Maths ";
checkGrade(M[i]); //Function called to check grade
}
count--;
}
}
return 0;
}
The above program stores data for all the students in respective marks array. The checkGrade function takes marks as input and displays the grade one by one. I would suggest you google to learn the concepts and solve some problems.
while (1)
{
cout << "Enter number and unit: ";
cin >> number;
cin >> unit;
if (unit == "in")
{
result = 2.54f * number;
cout << number << " Inches are " << result << " centimeters!" << endl;
}
else if (unit == "ft")
{
result = 0.3f * number;
cout << number << " Feet are " << result << " meters!" << endl;
}
else if (unit == "yd")
{
result = 0.9f * number;
cout << number << " Yards are " << result << " meters!" << endl;
}
else if (unit == "mi")
{
result = 1.6f * number;
cout << number << " Miles are " << result << " kilometers!" << endl;
}
else
{
cout << "\tINVALID!!!\n...the program will now exit...\n";
cin.clear();
cin >> unit;
cin >> number;
break;
}
}
cout << "Thank you for using English to Metric!\n" << endl;
return 0;}
What am I missing in order to get the last cout and exit the program without having to input anything after break?
The break in the else clause will exit the loop, but immediately before it you perform
cin.clear();
cin >> unit;
cin >> number;
break;
The cin >>s are why the program is waiting for additional input.
Without them, the cin.clear() is unnecessary, unless you plan to do additional cin operations later.
else
{
std::cin.clear(); // optional
break;
}
} // end while
std::cout << "Thank you\n";
Will work.
I'm currently taking a C/C++ programming class at my school. I am tasked with writing a piece of code that will ask the user how many numbers they would like averaged, then averages them. The program has to contain a for loop. The problem that I am having is that after the user has entered the "n" variable, if they type a character such as "a", the program will immediately spit out an answer as my average. I would like to find a way to prevent the user from entering characters so that my for loop can finish running and average the numbers properly. Here is my code:
{
int n, i = 1, x = 1;
double sum = 0, average, value;
cout << "\nHow many numbers do you want to average?: ";
cin >> n;
while (n < 1)
{
cout << "\nYou have entered an invalid number.\n";
cout << "\nHow many numbers do you want to average?: ";
cin.clear();
while (cin.get() != '\n');
cin >> n;
}
for (n; i <= n; i++)
{
cout << "\nEnter value: ";
cin >> value;
sum = sum + value;
}
average = sum / n;
cout << "\nThe average is: " << average << endl;
system("pause");
return 0;
}