Beginner Exercises

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

1
2
3
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?

1
2
3
if(between 0/100) {
//other tests
}
closed account (S6k9GNh0)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#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!";
              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:
1
2
3
4
5
6
7
8
9
10
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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <string>
#include <sstream>

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#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--;
}
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// 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 ?
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