Hi there guys I am having trouble with a particular input in my program. If you will run my program and enter the word "IT" in Enter Course, it loops and jumps to do you wish to return to the main menu. Please use Microsoft Visual C++ 6.0 Professional Edition in testing my code to be able to see my problem/

Here is my code: (Note* the line on red font is the ones im having trouble with)

#include<iostream>
#include<string>
#include<windows.h>
#include<fstream>
#include <iomanip>
#include<cctype>
#include<cstdlib>
#include <ctype.h>

using namespace std;

struct TF
{int units, punits, misc;};

struct studre
{
       char choice;
       string Sno;
       string Fname;
       string Mname;
       string Sname;
       string course;
       int year;
       int fees;
      
      
};

void ClearScreen();
void input();
int main()
{   
    
    
    int tuitiontotal;
    int i;
    int totalunits;
    studre level[100];
    int x;
    studre record[1000];
    TF amount[100];
    TF tuition;
    tuition.punits=800;
    tuition.misc=2000;
    char choice;
    ofstream output;

while(true)
{
cout<<"Enter number of students that will enroll (Max. of 5 students only)"<<endl;
cin>>x;

try
{
if(x>5||x<1)
{
throw x;
}

}
catch(int a)
{
ClearScreen();
cout<<"Invalid input. Please try again."<<endl;
continue;
}


output.open("record.txt", ios::app);
output<<"Enrollment and Tuition Fee Record"<<endl;
output<<endl;
output<<"Total Students: "<<x<<endl;
output<<endl;
output.close();
  
        

        

for (int i=0;i<x;i++)
{
        cin.ignore();
    
        try
        {
        cout<<"Student Number"<<"["<<i+1<<"]: "<<endl;
        getline(cin,record[i].Sno);
        if(record[i].Sno.length()>10||record[i].Sno.length()<10)
        throw record[i].Sno;
        }
        catch (string g)
        {
              while(record[i].Sno.length()>10||record[i].Sno.length()<10)
              {
              {
              ClearScreen();
              cout<<"Invalid input. Enter a 10 digit nuumber"<<endl;
              }
              getline(cin,record[i].Sno);
              }
        }
        cout<<"Enter First Name"<<"["<<i+1<<"]: "<<endl;
        getline(cin,record[i].Fname);
        
        cout<<"Enter Middle Name"<<"["<<i+1<<"]: "<<endl;
        getline(cin,record[i].Mname);
       
        cout<<"Enter Last Name"<<"["<<i+1<<"]: "<<endl;
        getline(cin,record[i].Sname);
       
        cout<<"Enter Course"<<"["<<i+1<<"]: "<<endl;
        cin>>record[i].course;
       
        cout<<"Enter Year"<<"["<<i+1<<"]: "<<endl;
        cin>>level[i].year;
       
        cout<<"Enter Units"<<"["<<i+1<<"]: "<<endl;
        cin>>amount[i].units;
        totalunits+=amount[i].units;
        
        ClearScreen();
}
       
        

 
cout<<"Total Number of Students Enrolled: "<<x<<endl;
tuitiontotal=(totalunits*tuition.punits)+(tuition.misc*x);
cout<<"Total Tuition Fee is:"<<"Php "<<tuitiontotal;
cout<<endl;


for (int j=0;j<x;j++)
{
output.open("record.txt", ios::app);

output<<"Student Number"<<"["<<j+1<<"]: "<<record[j].Sno<<endl;
output<<"First Name"<<"["<<j+1<<"]: "<<record[j].Fname<<endl;
output<<"Middle Name"<<"["<<j+1<<"]: "<<record[j].Mname<<endl;
output<<"Last Name"<<"["<<j+1<<"]: "<<record[j].Sname<<endl;
output<<"Course"<<"["<<j+1<<"]: "<<record[j].course<<endl;
output<<"Year"<<"["<<j+1<<"]: "<<level[j].year<<endl;
output<<"Units"<<"["<<j+1<<"]: "<<amount[j].units<<endl;
output<<"Tuition"<<"["<<j+1<<"]: "<<((amount[j].units)*(tuition.punits))+tuition.misc<<endl;
output<<endl;
output.close();
}

output.open("record.txt", ios::app);
output<<"Total Tuition Fee for "<<x<<" students:"<<"Php: "<<tuitiontotal<<endl;
output<<"End of Record"<<endl;
output.close();

while(choice!='Y'&&choice!='N')
{
cout<<"Do you wish to return to the Main Menu?(Y/N):";
cin>>choice;
ClearScreen();
choice=toupper(choice);
}

if(choice=='Y')
{
main();
}
else if(choice=='N')
exit(1);

system ("pause");
return 0;
    
}
}





void ClearScreen()
  {
  HANDLE hStdOut;
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  DWORD count;
  DWORD cellCount;
  COORD homeCoords = { 0, 0 };
  hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
  if (hStdOut == INVALID_HANDLE_VALUE) return;
  if (!GetConsoleScreenBufferInfo( hStdOut, &csbi ))
  return;
  cellCount = csbi.dwSize.X *csbi.dwSize.Y;
  if (!FillConsoleOutputCharacter(
    hStdOut,
    (TCHAR) ' ',
    cellCount,
    homeCoords,
    &count
    )) return;
  if (!FillConsoleOutputAttribute(
    hStdOut,
    csbi.wAttributes,
    cellCount,
    homeCoords,
    &count
    )) return;
  SetConsoleCursorPosition( hStdOut, homeCoords );
  }

Anyone there who has a fix for this??? Help me please...

The problem is here -> cin >> record[i].course; . It should be getline(cin, record[i].course);

I used getline before just like what I did for the F.Name,M.Name and S.Name but still it didn't work. below is my updated code I changed it back to getline(cin,record.course but still not working. When I run it in bloodshed dev c++ it works but in microsoft visual c++ 6.0 the error I mentioned above occurs.


#include<iostream>
#include<string>
#include<windows.h>
#include<fstream>
#include <iomanip>
#include<cctype>
#include<cstdlib>
#include <ctype.h>

using namespace std;

struct TF
{int units, punits, misc;};

struct studre
{
       char choice;
       string Sno;
       string Fname;
       string Mname;
       string Sname;
       string course;
       int year;
       int fees;
      
      
};

void ClearScreen();
void input();
int main()
{   
    
    
    int tuitiontotal;
    int i;
    int totalunits;
    studre level[100];
    int x;
    studre record[1000];
    TF amount[100];
    TF tuition;
    tuition.punits=800;
    tuition.misc=2000;
    char choice;
    ofstream output;

while(true)
{
cout<<"Enter number of students that will enroll (Max. of 5 students only)"<<endl;
cin>>x;

try
{
if(x>5||x<1)
{
throw x;
}

}
catch(int a)
{
ClearScreen();
cout<<"Invalid input. Please try again."<<endl;
continue;
}


output.open("record.txt", ios::app);
output<<"Enrollment and Tuition Fee Record"<<endl;
output<<endl;
output<<"Total Students: "<<x<<endl;
output<<endl;
output.close();
  
        

        

for (int i=0;i<x;i++)
{
        cin.ignore();
    
        try
        {
        cout<<"Student Number"<<"["<<i+1<<"]: "<<endl;
        getline(cin,record[i].Sno);
        if(record[i].Sno.length()>10||record[i].Sno.length()<10)
        throw record[i].Sno;
        }
        catch (string g)
        {
              while(record[i].Sno.length()>10||record[i].Sno.length()<10)
              {
              {
              ClearScreen();
              cout<<"Invalid input. Enter a 10 digit nuumber"<<endl;
              }
              getline(cin,record[i].Sno);
              }
        }
        cout<<"Enter First Name"<<"["<<i+1<<"]: "<<endl;
        getline(cin,record[i].Fname);
        
        cout<<"Enter Middle Name"<<"["<<i+1<<"]: "<<endl;
        getline(cin,record[i].Mname);
       
        cout<<"Enter Last Name"<<"["<<i+1<<"]: "<<endl;
        getline(cin,record[i].Sname);
       
        cout<<"Enter Course"<<"["<<i+1<<"]: "<<endl;
        getline(cin,record[i].course); //in this code I already returned getline for the Enter Course but still no luck :(
       
        cout<<"Enter Year"<<"["<<i+1<<"]: "<<endl;
        cin>>level[i].year;
       
        cout<<"Enter Units"<<"["<<i+1<<"]: "<<endl;
        cin>>amount[i].units;
        totalunits+=amount[i].units;
        
        ClearScreen();
}
       
        

 
cout<<"Total Number of Students Enrolled: "<<x<<endl;
tuitiontotal=(totalunits*tuition.punits)+(tuition.misc*x);
cout<<"Total Tuition Fee is:"<<"Php "<<tuitiontotal;
cout<<endl;


for (int j=0;j<x;j++)
{
output.open("record.txt", ios::app);

output<<"Student Number"<<"["<<j+1<<"]: "<<record[j].Sno<<endl;
output<<"First Name"<<"["<<j+1<<"]: "<<record[j].Fname<<endl;
output<<"Middle Name"<<"["<<j+1<<"]: "<<record[j].Mname<<endl;
output<<"Last Name"<<"["<<j+1<<"]: "<<record[j].Sname<<endl;
output<<"Course"<<"["<<j+1<<"]: "<<record[j].course<<endl;
output<<"Year"<<"["<<j+1<<"]: "<<level[j].year<<endl;
output<<"Units"<<"["<<j+1<<"]: "<<amount[j].units<<endl;
output<<"Tuition"<<"["<<j+1<<"]: "<<((amount[j].units)*(tuition.punits))+tuition.misc<<endl;
output<<endl;
output.close();
}

output.open("record.txt", ios::app);
output<<"Total Tuition Fee for "<<x<<" students:"<<"Php: "<<tuitiontotal<<endl;
output<<"End of Record"<<endl;
output.close();

while(choice!='Y'&&choice!='N')
{
cout<<"Do you wish to return to the Main Menu?(Y/N):";
cin>>choice;
ClearScreen();
choice=toupper(choice);
}

if(choice=='Y')
{
main();
}
else if(choice=='N')
exit(1);

system ("pause");
return 0;
    
}
}





void ClearScreen()
  {
  HANDLE hStdOut;
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  DWORD count;
  DWORD cellCount;
  COORD homeCoords = { 0, 0 };
  hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
  if (hStdOut == INVALID_HANDLE_VALUE) return;
  if (!GetConsoleScreenBufferInfo( hStdOut, &csbi ))
  return;
  cellCount = csbi.dwSize.X *csbi.dwSize.Y;
  if (!FillConsoleOutputCharacter(
    hStdOut,
    (TCHAR) ' ',
    cellCount,
    homeCoords,
    &count
    )) return;
  if (!FillConsoleOutputAttribute(
    hStdOut,
    csbi.wAttributes,
    cellCount,
    homeCoords,
    &count
    )) return;
  SetConsoleCursorPosition( hStdOut, homeCoords );
  }

When I run it in bloodshed dev c++ it works but in microsoft visual c++ 6.0 the error I mentioned above occurs.

Honestly, I don't know why this happens. I was able to compile and succesfully run the code in your last post
both with Code::Blocks/g++ and Visual C++ 2008 Express. Maybe someone else could shed some light...

I'm pretty sure the problem is coming from the fact that you are mixing input types. What happens if you change line 82 to

cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n' );

You will have to include the <limits> header for this to work

I'm pretty sure the problem is coming from the fact that you are mixing input types.

Indeed, that could be a problem. The cleanest solution to this is to only use getline for input:

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int get_int()
{
    string input;
    int ret = 0;

    getline(cin, input);

    stringstream(input) >> ret;

    return ret;
}

int main()
{
    string name;
    int id;
    int age;

    cout << "enter id: ";
    id = get_int();

    cout << "enter name: ";
    getline(cin, name);

    cout << "enter age: ";
    age = get_int();

    cout << endl
         << "id: "   << id   << endl
         << "name: " << name << endl
         << "age: "  << age  << endl;

    cout << "\n(hit enter to quit...)";
    cin.get();

    return 0;
}

Though, this doesn't explain why the behaviour varies among compilers...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.