🔒 Locked
Qns
Advertisement
Original Post
modify the program to use a for loop. #include using namespace std; int main() { int total, number, count, value; count = total = 0; // display prompt cout << "Enter a number:\n "; cin >> number; // input a number while (count < number) { cout << "Enter a value:\n "; cin >> value; total += value; count++; } // display total cout << "Total is " << total << endl; cin.ignore(); cin.ignore(); } How to solve this??
modify the program to use a switch statement.
cin >> grade ;
if (grade == 'A' || grade == 'a')
cout << "Excellent Work!!" << endl;
else if (grade == 'B' || grade == 'b')
cout << "Good Work!!" << endl;
else if (grade == 'C' || grade == 'c')
cout << "Average Work!!" << endl;
else if (grade == 'D' || grade == 'd')
cout << "Passable Work!!" << endl;
else
cout << "Re-submit your work" << endl;
cin >> grade ;
if (grade == 'A' || grade == 'a')
cout << "Excellent Work!!" << endl;
else if (grade == 'B' || grade == 'b')
cout << "Good Work!!" << endl;
else if (grade == 'C' || grade == 'c')
cout << "Average Work!!" << endl;
else if (grade == 'D' || grade == 'd')
cout << "Passable Work!!" << endl;
else
cout << "Re-submit your work" << endl;
int i = 5;switch (i) { case 5: cin >> grade ; if (grade == 'A' || grade == 'a') cout << "Excellent Work!!" << endl; else if (grade == 'B' || grade == 'b') cout << "Good Work!!" << endl; else if (grade == 'C' || grade == 'c') cout << "Average Work!!" << endl; else if (grade == 'D' || grade == 'd') cout << "Passable Work!!" << endl; else cout << "Re-submit your work" << endl;}
Quote:
Original post by rozz666 int i = 5;switch (i) { case 5: cin >> grade ; if (grade == 'A' || grade == 'a') cout << "Excellent Work!!" << endl; else if (grade == 'B' || grade == 'b') cout << "Good Work!!" << endl; else if (grade == 'C' || grade == 'c') cout << "Average Work!!" << endl; else if (grade == 'D' || grade == 'd') cout << "Passable Work!!" << endl; else cout << "Re-submit your work" << endl;}
lol.
Quote:Well, you have the count variable there, that would do nicely as a loop variable...
Original post by mugen82
modify the program to use a for loop.
[snip]
How to solve this??
Quote:Remember you can cascade case statements in a switch, something like:
Original post by mugen82
modify the program to use a switch statement.
[snip]
switch(num){ case 1: case 42: cout << "num is 1 or 42" << endl; break;}
switch( grade )
{
case A:
cout << "Excellent Work!!\n";
break;
case B:
cout << "Good Work!!\n";
break;
case C:
cout << "Average Work!!\n";
break;
case D:
cout << "Passable Work!!\n";
break;
default:
cout << "Re-submit your work.\n";
break;
}
is this correct?
{
case A:
cout << "Excellent Work!!\n";
break;
case B:
cout << "Good Work!!\n";
break;
case C:
cout << "Average Work!!\n";
break;
case D:
cout << "Passable Work!!\n";
break;
default:
cout << "Re-submit your work.\n";
break;
}
is this correct?
Quote:
Original post by Evil Steve Quote:Well, you have the count variable there, that would do nicely as a loop variable...
Original post by mugen82
modify the program to use a for loop.
[snip]
How to solve this??Quote:Remember you can cascade case statements in a switch, something like:
Original post by mugen82
modify the program to use a switch statement.
[snip]
*** Source Snippet Removed ***
Quote:
Original post by mugen82
switch( grade )
{
case A:
cout << "Excellent Work!!\n";
break;
case B:
cout << "Good Work!!\n";
break;
case C:
cout << "Average Work!!\n";
break;
case D:
cout << "Passable Work!!\n";
break;
default:
cout << "Re-submit your work.\n";
break;
}
is this correct?
1) Text needs to be quoted, or else the compiler will look for variables (or functions, or a few other sorts of things) with the given name. Even if you had such variables (e.g. 'int A;'), the compiler only accepts constant values for cases of a switch statement. You want to put the grades in question in single quotes, to indicate that they are single characters of text, e.g. case 'A':.
2) Handle the lowercase possibilities, too. Everything is case-sensitive unless you do the work yourself.
Quote:
Original post by mugen82
modify the program to use a for loop.
#include
using namespace std;
int main()
{
int total, number, count, value;
count = total = 0;
// display prompt
cout << "Enter a number:\n ";
cin >> number; // input a number
while (count < number)
{
cout << "Enter a value:\n ";
cin >> value;
total += value;
count++;
}
// display total cout << "Total is " << total << endl;
cin.ignore();
cin.ignore();
}
How to solve this??
#include <iostream> using namespace std; int main() { int total, number, count, value; count = total = 0; // display prompt cout << "Enter a number:\n "; cin >> number; // input a number for(;;) if (count >= number) break; else { cout << "Enter a value:\n "; cin >> value; total += value; count++; } // display total cout << "Total is " << total << endl; cin.ignore(); cin.ignore(); }Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement