Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 243
  • Last Modified:

Trouble using for loop with an array as well as reading in values.

For some reason I can't get this program to work.  It has something to do with how I have setup the for loop nested inside that is designed to read in 5 integer values.  After it gets done reading in the 5 values I get a logic error that prints out like this:    

Enter Name: Enter Class:

Instead of just having it like it was the first time it runs through:

Enter Name: (Name entered)
Enter Class: (Class entered)
and so on...

Here is the code for the program so far:

#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

char Name[5][20];
char Class[5][20];
char Teacher[5][20];
int      Grades[5];

void main()
{
 
  system("cls");

  cout << "This program creates a sheet of test results for 5 students." << endl << endl;


      for( int i = 0; i < 5; i++ )
      {
            cout << "Enter Name: ";
            cin.getline( Name[i],20 );
            
            cout << "Enter Class: ";
            cin.getline( Class[i],20 );

            cout << "Enter Teacher: ";
            cin.getline( Teacher[i],20 );

            for( int j = 0; j < 5; j++ )
            {
                  cout << "Enter Grade: ";
                  cin >> Grades[j];
            }
      }
 
system("pause");
} // end of main()
0
tocanna
Asked:
tocanna
  • 4
  • 3
  • 3
  • +3
3 Solutions
 
jkrCommented:
If you want to read an american-style grade (i.e. A-F), you need to use a char array (or string), since integers aren't suitable. Try

#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <string>
#include <math.h>

using namespace std;

char Name[5][20];
char Class[5][20];
char Teacher[5][20];
string      Grades[5];

void main()
{
 
  system("cls");

  cout << "This program creates a sheet of test results for 5 students." << endl << endl;


      for( int i = 0; i < 5; i++ )
      {
            cout << "Enter Name: ";
            cin.getline( Name[i],20 );
           
            cout << "Enter Class: ";
            cin.getline( Class[i],20 );

            cout << "Enter Teacher: ";
            cin.getline( Teacher[i],20 );

            for( int j = 0; j < 5; j++ )
            {
                  cout << "Enter Grade: ";
                  cin >> Grades[j];
            }
      }
 
system("pause");
}

BTW, you were using deprecated header files also.
0
 
tocannaAuthor Commented:
Thanks but we are not storing American style grades (IE: A-F), we are storing 5 integer values (IE: 90, 100), 75, etc. These are the headers our teacher has had us use and we also have not been using the 'using namespace std;' Any more help would be greatly appreciated.
0
 
jkrCommented:
Then you should be fine entering integer values with your code... *puzzled*
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
abithCommented:
try using flushall() before getting input
0
 
ikeworkCommented:
in each loop of "i" you overwrite the grades of the previous loop (student)
0
 
Infinity08Commented:
Did you mean to have :

        int      Grades[5][5];

ie. 5 grades per student ?

If so, also change this line :

                  cin >> Grades[j];

to :

                  cin >> Grades[i][j];

to read the j-th grade of the i-th student.
0
 
itsmeandnobodyelseCommented:
>>>> After it gets done reading in the 5 values I get a logic error that prints out like this:    

Are you using VC6 compiler? If yes, there is a bug regarding synchronization of 'cin.getline' and 'cin>>'.

As the std template headers are source code only (no libraries involved) you can fix the bug yourself, see http://support.microsoft.com/support/kb/articles/Q240/0/15.ASP

Regards, Alex

0
 
tocannaAuthor Commented:
tried changing int Grades[5] to in Grades[5][5];
and cin >> Grades[i][j];

also did the fix alex posted, still getting the same problem. It runs through once and soon as I enter the fifth grade for the first student I see this:

Enter Name: Enter Class:

Then it seems to work normally (allows me to enter the class, teacher and 5 grades)  but just skips the input name.
0
 
Infinity08Commented:
You shouldn't mix getline and normal stream input (>>). That's what's causing your problem : the last enter of the grades gets read by the next getline, and seen as an empty line.

Instead, also use getline to read the grades into a temporary string, and then use atoi to convert the string to an int :

        http://www.cplusplus.com/reference/clibrary/cstdlib/atoi.html


Btw : you shouldn't use the <iostream.h> and <iomanip.h> headers - they are deprecated. Use <iostream> and <iomanip> instead. You'll also have to add this line just after the headers :

        using namespace std;

Also, main has to return an int (right now you return void).

0
 
itsmeandnobodyelseCommented:
The following works:

#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <string.h>
#include <cmath>

using namespace std;

char Name[5][20];
char Class[5][20];
char Teacher[5][20];
int      Grades[5][5];

int main()
{
   
    system("cls");
   
    cout << "This program creates a sheet of test results for 5 students." << endl << endl;
   
    char Grade[10];
    for( int i = 0; i < 5; i++ )
    {
        cout << "Enter Name: ";
        cin.getline( Name[i],20 );
       
        cout << "Enter Class: ";
        cin.getline( Class[i],20 );
       
        cout << "Enter Teacher: ";
        cin.getline( Teacher[i],20 );
       
        for( int j = 0; j < 5; j++ )
        {
            cout << "Enter Grade: ";
            cin.getline(Grade,10 );
           
            Grades[i][j] = atoi(Grade);
        }
    }
   
    system("pause");
     return 0;
} // end of main

I replaced  the cin>> and made a further getline on a temporary string with a 'atoi' instead. The last 'enter' after cin>> spoils the first getline of the next iteration.

Regards, Alex

Regards, Alex
0
 
Infinity08Commented:
Thanks for putting it in code, Alex ;)
0
 
tocannaAuthor Commented:
Thanks again guys, you helped me out a ton. Thanks for putting up with my lack of knowledge, I am just starting out.
0
 
itsmeandnobodyelseCommented:
>>>> Thanks for putting it in code, Alex ;)

You may not believe me, but I made the code after I read tocanna's comment that it still doesn't work and posted it *before* I read your comment. It is one more time where you and me have identical solutions ... ;-)
0
 
Infinity08Commented:
>> You may not believe me

That's not what I meant ... I just didn't have the time to adapt and test the code ...


>> It is one more time where you and me have identical solutions ... ;-)

Just shows that the solution is good eh :)
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

  • 4
  • 3
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now