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...
#include <iostream>
#include <string>
#include <sstream>
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!";
elseif (grade >= 80) std::cout << "Congratulations! You made a B!";
elseif (grade >= 70) std::cout << "Doh! You made a C!";
elseif (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();
}
This is just style but I suggest you having a more readable line style:
1 2 3 4 5 6 7 8 9 10
if (grade >= 90)
std::cout << "Congratulations! You made an A!";
elseif (grade >= 80)
std::cout << "Congratulations! You made a B!";
//...
else
{
std::cout << "Fail! Please enter a valid grading number: ";
continue;
}
@ 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.
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.
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
1 2
draw newline
flush stdout
or std::cout << "\n" << std::flush;.
inb4 computerquip;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
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.
#include <iostream>
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.
#include <iostream>
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:
1 2 3 4 5 6 7 8
int user = 20, gullible = 20;
while (user == gullible) {
// ...
if (gullible == 10 || user == 10)
break;
gullible--;
user--;
}
// Pancakes Glutton
#include <iostream.h>
#include <conio.h>
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 "<<i<<":";
cin>>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<pers[i]) // calculez max
max=pers[i];
}
for(i=1;i<=5;i++)
{
if(min==pers[i])
cout<<"\nPersoan "<<i<<" ate the least pancakes! ("<<min<<")";
if(max==pers[i])
cout<<"\nPersoan "<<i<<" ate the most pancakes! ("<<max<<")";
}
for(i=1;i<=5;i++)
for(int j=i+1;j<=5;j++)
if(pers[j]<pers[i])
{
int aux=pers[j];
pers[j]=pers[i];
pers[i]=aux;
}
for(i=1;i<=5;i++)
cout<<pers[i]<<" ";
getch();
}
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 ?
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.