2

I am having some trouble getting a C++ logic loop working. From testing, I believe it is a error in the Grades() function loop.

Basically the program is supposed to get the Names and Test Scores, which it appears to do, but the program exits right after that and I am not sure why. Just wanting to see if anyone else noticed an error in the function that could be causing the program to terminate prematurely.

// Grades
void Grades(char names[][length], double grades[][4])
{
    for(int i = 0; i < amount; i++)
    {
        cout << "Name: ";
        cin.ignore();

        cin.getline(names[i],length, '\n');

        cout << "Test Scores: ";

        for(int k = 0; k < 4; k++)
        {
            int num = 0;

            while(!(cin >> num) ||
                  num  < 0 ||
                  num > 100 ) 
            {
                cout << "invalid entry." << endl;
            }

            grades[i][k] = num;
        }
    }
}
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Austin Cherry
  • 112
  • 2
  • 10

2 Answers2

3

Something that bites every C++ beginner… iostream errors are "sticky" and remain until you call clear on the stream.

while(!(cin>> num) || num  < 0 || num > 100 ) 
{
    cout << "invalid entry." << endl;
    cin.clear(); // reset the error so cin evaluates to true
    cin.ignore( -1 ); // ignore the offending input
}

Also, the cin.ignore() before getline will ignore the first letter of the first name.

You should use std::string instead of char arrays.

Other than that, the program essentially works as long as the input is formatted properly. Here is the output on my system. Note that Bob's initial is cut off, and Bob and Jane have uninitialized characters after their names because the character array isn't being treated as a C-style string.

Student: ob_???_
Average Score: 60.00 Grade Letter: D

Student: jane   ???_?
Average Score: 70.00 Grade Letter: C

Student: mary
Average Score: 80.00 Grade Letter: B

Student: todd
Average Score: 90.00 Grade Letter: A

Student: alice
Average Score: 100.00 Grade Letter: A

Strangely, though, ideone.com did run the program but didn't show its output, for some reason. I have no idea.

As Evan mentions, the Windows shell exits by default if you run a command-line program by double-clicking. Run from a preexisting interactive shell to see your output. system( "pause" ); is unportable and very bad practice.

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
1

What compiler/operating system are you using?

Sometimes it's necessary to add a system("pause"); call before you return from main. This is often true if you're compiling from an IDE than runs the program directly. You could avoid the problem by running it directly from the command line/bash/etc.

Evan Cordell
  • 4,108
  • 2
  • 31
  • 47
  • Xcode 4, Mac OSX 10.6.8. Did not even think of that, that fixed it. Thanks. – Austin Cherry Sep 16 '11 at 03:02
  • 2
    Much better than the platform specific `system("pause")`... `char c; cin >> c;` – Ed S. Sep 16 '11 at 03:11
  • @AC: Wait, how were you seeing the program's other prompts and giving it input except from XCode's console window? I'm confused. – Potatoswatter Sep 16 '11 at 03:15
  • I was doing it from the xcode output window. I went ahead and did it from a terminal window and it ran through and I was able to fix the bug I had with the code. – Austin Cherry Sep 16 '11 at 03:33
  • @Ed: Good point! `system("pause");` should definitely never be used unless testing locally, or if the target OS is known and will not change. – Evan Cordell Sep 16 '11 at 03:43
  • @Evan: Knowing the target OS is no help. You might assume it will work for all Windows users, but it wont work for me because my cli is not cmd, it is Powershell, where "pause" is not a valid command. – Benjamin Lindley Sep 16 '11 at 12:31
  • @Benjamin: You're right, shell dependency would be more accurate than OS. Pausing is different in bash and zsh and ksh, too. The point is that while I personally would never use a call like this in something other people would see, with enough knowledge of the deployment environment, it *could* be used. – Evan Cordell Sep 16 '11 at 16:30