Skip to main content
gamedev.net
🔒 Locked

Qns

Started by mugen82 Feb 26, 2009 at 2:01 AM 9 replies 1.2k views
Original Post
mugen82
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??
mugen82
mugen82
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;
Zakwayda
Zakwayda
Are these homework questions?
rozz666
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;}
Dunge
Dunge
Passable Work rozz666 :)

It actually do look like two (very simple) homework assignments and a student who want to go the easy way.
dave
dave
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.
Evil Steve
Evil Steve
Quote:
Original post by mugen82
modify the program to use a for loop.

[snip]

How to solve this??
Well, you have the count variable there, that would do nicely as a loop variable...

Quote:
Original post by mugen82
modify the program to use a switch statement.

[snip]
Remember you can cascade case statements in a switch, something like:
switch(num){   case 1:   case 42:      cout << "num is 1 or 42" << endl;       break;}
mugen82
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?



Quote:
Original post by Evil Steve
Quote:
Original post by mugen82
modify the program to use a for loop.

[snip]

How to solve this??
Well, you have the count variable there, that would do nicely as a loop variable...

Quote:
Original post by mugen82
modify the program to use a switch statement.

[snip]
Remember you can cascade case statements in a switch, something like:
*** Source Snippet Removed ***


Ezbez
Ezbez
That will work, but it doesn't cover the cases of grade being a lower case number. Look back up at what Evil Steve said to see how to do the equivalent to an "or" in a switch statement.
Zahlman
Zahlman
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.

Rattenhirn
Rattenhirn
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.