Solved

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

Posted on 2007-11-13
14
234 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
  • 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

705 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now