Hey guys what is wrong in this code?
i am getting crazy
when its start and you press numb 1 ( its not finished ) this program ask all of question in one line without wait to enter name... and lname.. and major... it's ask all of them together :|
#include <iostream>
#include <cstdlib>
usingnamespace std;
class NewStudent{
public:
char Name[20],Lname[30],Major[30],Location[4];
int Grade=0,Age=0;
};
int New(){
NewStudent STU;
cout << "Please Enter The Name Of The Student: ";
cin.getline(STU.Name,19);
cout << "Please Enter The Last Name Of The Student: ";
cin.getline(STU.Lname,29);
cout << "Please Enter The Age Of The Student: ";
cin >> STU.Age;
cout << "Please Enter The Location Of The Student: ";
cin.getline(STU.Location,3);
cout << "Please Enter The Major Of The Student: ";
cin.getline(STU.Major,29);
cout << "Please Enter The Grade Of The Student: ";
cin >> STU.Grade;
}
int main()
{
int chose;
cout << "WelCome :)\n" << endl;
cout << "(1) -> Enter New Student. "
<< "\n(2) -> Delete A Student. "
<< "\n(3) -> Ask For Information Of a Student. "
<< "\n(4) -> Ask For Average Of a Student."
<< "\n(5) -> About Program.\n"
<< "\nPlease Select One Of Numbers > ";
cin >> chose;
switch(chose)
{
case 1:
{
New();
break;
}
case 2:
{
cout << "\n\nDelete A Student." <<endl;
break;
}
case 3:
{
cout << "\n\nAsk For Information Of a Student." <<endl;
break;
}
case 4:
{
cout << "\n\nAsk For Average Of a Student." <<endl;
break;
}
case 5:
{
cout << "\n\nAbout Program." <<endl;
break;
}
}
return 0;
}
only before line 16 and lon line 27 to clear your input buffer, but why not just use strings they've got all the advantages.
why should we add this? :/ Is it a problem of C++?? or i should try another codes?
no it's just because the way you're using your stream causes cin to retain some content in it's buffer, when that occurs when cin encounters another cin.getline() it will use the leftovers instead of reading from the keyboard, i would urge you to use strings instead.
#include <iostream>
#include <string>
usingnamespace std;
class NewStudent{
public:
string Name,Lname,Major,Location;
int Grade=0,Age=0;
};
int New(){
NewStudent STU;
cout << "Please Enter The Name Of The Student: ";
cin>>STU.Name;
cout << "Please Enter The Last Name Of The Student: ";
cin>>STU.Lname;
cout << "Please Enter The Age Of The Student: ";
cin >> STU.Age;
cout << "Please Enter The Location Of The Student: ";
cin>>STU.Location;
cout << "Please Enter The Major Of The Student: ";
cin>>STU.Major;
cout << "Please Enter The Grade Of The Student: ";
cin >> STU.Grade;
}
int main()
{
int chose;
cout << "WelCome :)\n" << endl;
cout << "(1) -> Enter New Student. "
<< "\n(2) -> Delete A Student. "
<< "\n(3) -> Ask For Information Of a Student. "
<< "\n(4) -> Ask For Average Of a Student."
<< "\n(5) -> About Program.\n"
<< "\nPlease Select One Of Numbers > ";
cin >> chose;
switch(chose)
{
case 1:
{
New();
break;
}
case 2:
{
cout << "\n\nDelete A Student." <<endl;
break;
}
case 3:
{
cout << "\n\nAsk For Information Of a Student." <<endl;
break;
}
case 4:
{
cout << "\n\nAsk For Average Of a Student." <<endl;
break;
}
case 5:
{
cout << "\n\nAbout Program." <<endl;
break;
}
}
return 0;
}
this is for a school ok?
i wanna to do some thing that if person press 1 start to add student and when press EOF something like 999 or -1 finish the entering... beside that, i wanna to save this on a file in order that if he/she press number 3 and chose to search by name or lname... be able to find student from the file. but every time i start to add new student it replaced to the last student :/
to sum up:
1- i don't know how to add names over and over in the file without losing last data
2- i don't know how to search in the file by the name or lname :(
oh i have number 3 too: i don't know how to delete a information of a student that i have entered already
#include <iostream>
#include <string>
#include <fstream>
usingnamespace std;
class NewStudent{
public:
string Name,Lname,Major,Location;
int Grade=0,Age=0;
};
int Adder = 0;
NewStudent STU[255];
int New(){
for(Adder;Adder< 55;Adder++){
cout << "Please Enter The Name Of The Student: ";
cin >>STU[Adder].Name;
cout << "Please Enter The Last Name Of The Student: ";
cin >>STU[Adder].Lname;
cout << "Please Enter The Age Of The Student: ";
cin >> STU[Adder].Age;
cout << "Please Enter The Location Of The Student: ";
cin >>STU[Adder].Location;
cout << "Please Enter The Major Of The Student: ";
cin >>STU[Adder].Major;
cout << "Please Enter The Grade Of The Student: ";
cin >> STU[Adder].Grade;
ofstream fout ("output.txt");
fout << STU[Adder].Name << "," << STU[Adder].Lname << "," << STU[Adder].Age
<< "," << STU[Adder].Location << "," << STU[Adder].Major << ","
<< STU[Adder].Grade << endl;
fout.close();
cout << "\nYour Student Have Bean Saved";
}
}
int Delete(){
}
Fiind(){
cout << "You have Name(1) Or Lastname(2)";
int chose;
cin >> chose;
switch(chose)
{
case 1:
{
cout << "\n\nYou Have Name. So Please Enter The Name of Student That You Want" <<endl;
int n = STU.name.size();
for (int i=0;i<=n;i++)
break;
}
case 2:
{
cout << "\n\nYou Have Name. So Please Enter The Last Name of Student That You Want" <<endl;
break;
}
}
}
int Average(){
}
int main()
{
int chose;
cout << "(1) -> Enter New Student. "
<< "\n(2) -> Delete A Student. "
<< "\n(3) -> Ask For Information Of a Student. "
<< "\n(4) -> Ask For Numbers Of a Computer Student"
<< "\n(5) -> Ask For Numbers Of how many lives in esf\n"
<< "\nPlease Select One Of Numbers > ";
cin >> chose;
switch(chose)
{
case 1:
{
cout << "\n\nEnter New Student." <<endl;
New();
cout << "\n\n" << endl;
main();
break;
}
case 2:
{
cout << "\n\nDelete A Student." <<endl;
Delete();
break;
}
case 3:
{
cout << "\n\nAsk For Information Of a Student." <<endl;
Fiind();
break;
}
case 4:
{
cout << "\n\nAsk For Numbers Of a Computer Student." <<endl;
break;
}
case 5:
{
cout << "\n\nAsk For Numbers Of how many lives in esf." <<endl;
break;
}
}
return 0;
}