How does this not work?

It was working before, I don't know what I did, but i've searched and searched and can't find any reason for this not to work. I don't get any errors, but when it Should say "Student 1 is now set to name" It says "Student 1 is now set to ".
I can post the full code if needed, but it's kind of long.



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
123
124
125
126
127
128
129
130
131
#include <iostream>
#include <string>
#include<conio.h>
#include <cstdlib>
using namespace std;


class STUDENTS{

public:

    void getname1(string x){
     name1 = x;
    }
    void getname2(string y){
     name2 = y;
    }
    void getname3(string z){
     name3 = z;
    }
    string setname1(){
    return name1;
    }
    string setname2(){
    return name2;
    }
    string setname3(){
    return name3;
    }
private:
    string name1;
    string name2;
    string name3;


};


int main(){

char choice;
string name1;
string name2;
string name3;
string over;




cout << "Please choose a name to change first. Type [Exit] to close" << endl;
cout << "Student [1]: " << name1 << "\nStudent [2]: " << name2 << "\nStudent [3]: " << name3 << endl;
cin >> choice;
if(choice == '1'){
        STUDENTS inwin;
        system("cls");
        cout << "Type the name of the student" << endl;
        cin >> name1;
        inwin.getname1(name1);
        cout << name1 << " correct? y/n" << endl;
        cin >> choice;
}
    if(choice == 'y'){
        system("cls");
        cout << "Student [1]: " << name1 << " \nStudent [2]: " << name2 << " \nStudent [3]: " << name3 << endl;
        cout << "\n";
        cout << "\n";
        cout << "Student 1 is now set to " << name1 << endl;
}

    if(choice == 'n'){
        while(choice == 'n'){
        STUDENTS inwin;
        cout << "Type the name of the student" << endl;
        cin >> name1;
        inwin.getname1(name1);
        cout << name1 << " correct? y/n" << endl;
        cin >> choice;
}
        system("cls");
        cout << "Student [1]: " << name1 << " \nStudent [2]: " << name2 << " \nStudent [3]: " << name3 << endl;
        cout << "\n";
        cout << "\n";
        cout << "Student 1 is now set to " << name1 << endl;

    }

if(choice == '2'){
        STUDENTS inwin;
        system("cls");
        cout << "Type the name of the student" << endl;
        cin >> name2;
        inwin.getname2(name2);
        cout << name2 << " correct? y/n" << endl;
        cin >> choice;
}
    if(choice == 'y'){
        system("cls");
        cout << "Student [1]: " << name1 << " \nStudent [2]: " << name2 << " \nStudent [3]: " << name3 << endl;
        cout << "\n";
        cout << "\n";
        cout << "Student 1 is now set to " << name2 << endl;
}

    if(choice == 'n'){
        while(choice == 'n'){
        STUDENTS inwin;
        cout << "Type the name of the student" << endl;
        cin >> name2;
        inwin.getname1(name2);
        cout << name2 << " correct? y/n" << endl;
        cin >> choice;
}
        system("cls");
        cout << "Student [1]: " << name1 << " \nStudent [2]: " << name2 << " \nStudent [3]: " << name3 << endl;
        cout << "\n";
        cout << "\n";
        cout << "Student 2 is now set to " << name2 << endl;

    }








    return 0;

}
Last edited on
where do you initialize name1 variable?
It seems that name1 isn't initialized yet. Recheck where you initialized it.
Last edited on
I posted the full code, I thought I had initialized it, but maybe not, this is only my second day of coding.
There is a fallthrough:
you have 2 conditions where choice == 'y' is tested. Both will be executed. As second one clears screen, you will only see it no matter what. And it uses name2.
Delete all system calls and see for yourself. I have ssen it immideatly because cls will not work fo me or anybody using linux.
How do you recommend fixing this? I thought since they were in two different bodys things like that would be fine
It would be much easier for both you and us to follow the logic of your code if you adopted a sensible indentation style.
Alright, will do. Didn't know there was a such thing as a coding style, but I'll start coding in one from now on.
http://en.wikipedia.org/wiki/Indent_style

This can be a subject for religious wars, so tread carefully :)
Dominic4774, you can solve the problem like this:
delete the closing brackets in lines 61 and 95 then put them in lines 86 and 120.
this way you will solve the problem MiiNiPaa mentioned.
Last edited on
Thanks
Topic archived. No new replies allowed.