Need Help

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 :|

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <cstdlib>

using namespace 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;
}
closed account (SECMoG1T)
add this before line 16 and on line 27;
cin.ignore(100,'\n');

i would also advice you to use strings instead to avoid all these problems.
Last edited on
tnxX For Answer

you mean add this code before all of // cin.getline()'s // ?
or just add before 16 and never need to add again?

why should we add this? :/ Is it a problem of C++?? or i should try another codes?
what dose this code do?

sorry i have lot of question :(
Last edited on
closed account (SECMoG1T)
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.
Last edited on
closed account (SECMoG1T)
try this

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <string>

using namespace 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;
}
TNXx For patience and your evident.

now i have a new Problem :))

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

this is my new code:
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
#include <string>
#include <fstream>

using namespace 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;
}



pleasssss someone help me :'(
Last edited on
Topic archived. No new replies allowed.