String input prolem
While I a run this code without using loop, it works perfect. But using loop, there is a problem. Can anyone fix this?
Thanks in advance.
With loop(need loop to take several student's data):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
#include <iostream>
#include<string>
using namespace std;
class std_info{
public:
string name;
string id;
int credit;
float cgpa;
};
int main()
{
std_info student;
int option;
do{
cin>>option;
cout<<"Enter student name: ";
getline(cin, student.name);
cout<<"Enter id: ";
getline(cin, student.id);
cout<<"Enter credit: ";
cin>>student.credit;
cout<<"Entmer cgpa: ";
cin>>student.cgpa;
cout<<"Nmae: "<<student.name<<endl;
cout<<"Id: "<<student.id<<endl;
cout<<"Credit: "<<student.credit<<endl;
cout<<"Cgpa: "<<student.cgpa<<endl;
}while(option == 1);
return 0;
}
|
Without loop(take just one student's data):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
#include <iostream>
#include<string>
using namespace std;
class std_info{
public:
string name;
string id;
int credit;
float cgpa;
};
int main()
{
std_info student;
cout<<"Enter student name: ";
getline(cin, student.name);
cout<<"Enter id: ";
getline(cin, student.id);
cout<<"Enter credit: ";
cin>>student.credit;
cout<<"Entmer cgpa: ";
cin>>student.cgpa;
cout<<"Nmae: "<<student.name<<endl;
cout<<"Id: "<<student.id<<endl;
cout<<"Credit: "<<student.credit<<endl;
cout<<"Cgpa: "<<student.cgpa<<endl;
return 0;
}
|
that loop should work ? what is the exact problem ?
The Problem is, without loop, it displays:
Enter student name:
Enter Id:
Enter credit:
Enter CGPA: |
But without, it displays:
Enter student name: Enter Id:
Enter credit:
Enter CGPA: |
But I need to use loop.
add cin.ignore()
after cin >> option;
Thanks a lot.. :)
Topic archived. No new replies allowed.