### Beginner Exercises

Pages: 1234... 16
 Any improvements on this?
You don't need to check twice the values:
 ``12345`` ``````if (grade >= 90 && grade <= 100) //... else if (grade >= 80) // surely smaller than 90 //... etc.``````

 ``123`` ``````if (temp >> grade) //... else std::cout << "Invalid number, please try again: "; // not an invalid number but invalid input ``````

closed account (S6k9GNh0)
The problem with that, is that if they enter 1234123 it will be accounted for and not stopped. And even then, it will be counted as a B. Any other way you can think of?

EDIT: I could make a check at the beginning I s'pose...
Last edited on
Check if it is in the range first, then check everything else, perhaps?

 ``123`` ``````if(between 0/100) { //other tests }``````
closed account (S6k9GNh0)
 ``123456789101112131415161718192021222324252627282930313233`` ``````#include #include #include int main(int argc, char ** argv) { std::cout << "Please enter the grade you recieved in programming: "; int grade; std::string input; for(;;) { getline(std::cin,input); //Recieve grade from user. std::stringstream temp(input); if (temp >> grade) { if (grade >= 0 && grade <= 100) { if (grade >= 90) std::cout << "Congratulations! You made an A!"; else if (grade >= 80) std::cout << "Congratulations! You made a B!"; else if (grade >= 70) std::cout << "Doh! You made a C!"; else if (grade >= 60) std::cout << "You suck! You made a D!"; else std::cout << "You fail at life with an F!"; } else { std::cout << "Fail! Please enter a valid grading number: "; continue; } std::cout << std::endl << grade; break; } else std::cout << "Invalid number, please try again: "; } std::cin.get(); }``````

Anything else?
Last edited on
This is just style but I suggest you having a more readable line style:
 ``12345678910`` ``````if (grade >= 90) std::cout << "Congratulations! You made an A!"; else if (grade >= 80) std::cout << "Congratulations! You made a B!"; //... else { std::cout << "Fail! Please enter a valid grading number: "; continue; }``````
I don't know if this would affect the size, but you could remove the int argc/char** argv since you aren't using them.
One thing; why use getchar() instead of std::cin.get()? getchar() is C...
closed account (S6k9GNh0)
@ Bazzy, I could but I changed it to show that that can be done that way. For one line cout lines, I see no problem with that plus it shortens the amount of lines produces by half and gives a clearn outlook. That's my opinion and sorry but I'm sticking by it. :D If they ever produce a .zip containing the test answers, I'll change it if requested too.

@ firedraco, it shouldn't matter at all. I'll read up on the specification later about it. Until then though, I'll take them out since it's useless code that will never be used in this example.

@ chrisname, I didn't think it mattered... Does it?

Actually, I read up on "How to keep your console open". I find that using the limits header unnecessary and I can't help but not find an explanation for it. If anyone would really like to explain this to me, I'm all ears.
Last edited on
Not really; but getchar() is C and std::cin.get() is C++; I was just being picky.

The std::numeric_limits<std::streamsize>::max thing, you mean? I can't remember, I gave a really good (I hope) explanation once... I'll try to find it.
closed account (S6k9GNh0)
That's the one. I wouldn't mind an explanation but I fail to find one myself.
closed account (S6k9GNh0)
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152`` ``````#include #include #include int main() { char s; std::string input; std::cout << "1 - Water" << std::endl; std::cout << "2 - Coke " << std::endl; std::cout << "3 - Sprite" << std::endl; std::cout << "4 - Root Beer" << std::endl; std::cout << "5 - Big Red" << std::endl; std::cout << "Please make a choice of your favorite beverage: "; bool bRun(true); while (bRun) { bRun = false; for(;;) { getline(std::cin, input); if (input.length() == 1) { s = input[0]; break; } } switch (s) { case '1': std::cout << "You chose meezu (Japanese pronounciation for Water)" << std::endl; break; case '2': std::cout << "You chose Coke. It's better in a glass bottle..." << std::endl; break; case '3': std::cout << "You chose Sprite. It tastes like alka setzer. Or maybe vise versa..." << std::endl; break; case '4': std::cout << "You chose Root Beer. Drink responsibly." << std::endl; break; case '5': std::cout << "You chose Big Red. It's smaller than my hand. How is it big?" << std::endl; break; default : bRun = true; std::cout << "Error. choice was not valid. Please make a valid selection: " << std::endl; break; } } std::cin.get(); }``````

Criticize it and I'll edit it. This was done pretty fast so I might have made a dumb error or two.
Last edited on
You could make it more machine like:

`std::cout << "1. Water \$1.50";`
and if you were really awesome you'd spend two hours working out how to make the text scroll with ncurses or something. That'd be awesome.

By the way, why put std::endl at the end of every cout statement? Why not put a '\n' then an std::flush before you exit (to make sure stdout is printed)? It won't make a noticeable difference, particularly not in a program like this, but on something bigger it will slow it down. std::endl is basically
 ``12`` ``````draw newline flush stdout``````

or
`std::cout << "\n" << std::flush;`.

inb4 computerquip;
 ``123456789101112131415`` ``````#include int main() { int i = 0; while (i != 5) { std::cout << "Please enter any number other than 5.\n"; std::cin >> i; } std::cout << "You " << /* "suck" << */ "win." << std::endl; return 0; }`````` ```Please enter any number other than 5. 4 Please enter any number other than 5. 5 You win.```

 ``123456789101112131415161718192021`` ``````#include int main() { int i = 0, j = 1; while (i != 5 && j <= 10) { std::cout << "Please enter any number other than 5.\n"; std::cin >> i; j++; } std::cout << "You " << /* "suck" << */ "win." << std::endl; return 0; }`````` ```Please enter any number other than 5. 1 Please enter any number other than 5. 1 Please enter any number other than 5. 1 Please enter any number other than 5. 1 Please enter any number other than 5. 1 Please enter any number other than 5. 1 Please enter any number other than 5. 1 Please enter any number other than 5. 1 Please enter any number other than 5. 1 Please enter any number other than 5. 1 You win.```

 ``123456789101112131415161718192021`` ``````#include int main() { int i, j = 1, n = i = 0; while (j <= 10) { std::cout << "Please enter any number other than " << n << ".\n"; std::cin >> i; if (i == n) break; j++; n++; } std::cout << "You " << /* "suck" << */ "win." << std::endl; return 0; }`````` ```Please enter any number other than 0. 9 Please enter any number other than 1. 9 Please enter any number other than 2. 9 Please enter any number other than 3. 9 Please enter any number other than 4. 9 Please enter any number other than 5. 9 Please enter any number other than 6. 9 Please enter any number other than 7. 9 Please enter any number other than 8. 9 Please enter any number other than 9. 9 You win.```

Alternatively for the while loop:
 ``12345678`` ``````int user = 20, gullible = 20; while (user == gullible) { // ... if (gullible == 10 || user == 10) break; gullible--; user--; }``````
Last edited on
I can't wait to start on the graduation program. Looks to be very interesting..
Random Post:

Meezu? Did you try to convert the romaji into American-like pronunciation? Because when I see that I pronounce it Meehzu >_>

I would just use the original romaji.
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455`` ``````// Pancakes Glutton #include #include void main() { int i,min,max,pers[10]; cout<<"How many pancakes did the following persons ate?\n"; for(i=1;i<=5;i++) { cout<<"Person "<>pers[i]; } min=pers[1]; max=pers[i]; for(i=2;i<=5;i++) { if(min>pers[i]) // calculez min min=pers[i]; if(max

hi everyone, my first post here :) .. my question is why is it when i started sorting the "number of pankes ate" the max stopped showing on execution ? been sitting here for an 1h now and cant figure it out , tnx for your time. (if i set the sorting part as a comment the max works fine)

PS: any ideea on how i can keep the number of the person so i can print it at the end ? i was thining of making another array (pers[20][20]) to keep the number aswell, but that sounds like alot of work :D any easier way ?
ok tnx, i will move it there then... the program was on the list , thought it was appropriate to post it here :)
@computerquip: Any idea on when you will be making the source files available?
closed account (S6k9GNh0)
Whenever I get around to it. I'm hesitating since I'm afraid people will look at it and not learn from it.
Maybe we could have that people, after trying the exercises themselves, to post their code and more expert programmers pointing out where that code can be improved.
Pages: 1234... 16