Solved

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

Posted on 2007-11-13
14
241 Views
Last Modified: 2013-12-14
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
Comment
Question by:tocanna
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +3
14 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 20277520
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
 

Author Comment

by:tocanna
ID: 20277659
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
 
LVL 86

Expert Comment

by:jkr
ID: 20277672
Then you should be fine entering integer values with your code... *puzzled*
0
Independent Software Vendors: 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!

 
LVL 5

Expert Comment

by:abith
ID: 20277977
try using flushall() before getting input
0
 
LVL 20

Expert Comment

by:ikework
ID: 20278058
in each loop of "i" you overwrite the grades of the previous loop (student)
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20278308
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
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 300 total points
ID: 20279965
>>>> 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
 

Author Comment

by:tocanna
ID: 20282230
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
 
LVL 53

Accepted Solution

by:
Infinity08 earned 200 total points
ID: 20282315
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
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 300 total points
ID: 20282979
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 20282997
Thanks for putting it in code, Alex ;)
0
 

Author Comment

by:tocanna
ID: 20283638
Thanks again guys, you helped me out a ton. Thanks for putting up with my lack of knowledge, I am just starting out.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20284174
>>>> 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
 
LVL 53

Expert Comment

by:Infinity08
ID: 20284202
>> 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

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question