Help with code?`

Hello,

I guess I'm not as savvy with while loops as I thought. I know that I have to declare a value outside of the scope to have the loop run. I just don't understand why this isn't working. Any pointers? One time when I ran this it compiled. It does everything right, until I enter a lower case 'n' it should be looping with both and I even used the topper function.


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
int main()
 17 {
 18 
 19 
 20     char person [NAME]; // variables store names of users
 21     char declare_m [MAJOR];
 22     char response = toupper(response);
 23 
 24     cout << "Enter name: " << endl; //gets user names and converts to upper
 25     cin.get(person, NAME, '\n');
 26     cin.ignore(100, '\n');
 27     person[0] = toupper(person[0]);
 28 
 29     int len1 = strlen(person);// capitalizes every word after a statement
 30     for (int i = 1; i < len1; ++i) //starts index 1
 31       {
 32        if(person[i] == ' ')
 33           person[i+1] = toupper(person[i+1]);
 34       }
 35     cout << "Hello " << person << endl;
 36 
 37 do
 38 {
 39 
 40     cout << "What's your major? " << endl; // get user major
 41     cin.get (declare_m, MAJOR,'\n');
 42     declare_m[0] = toupper(declare_m[0]);
 43     cin.ignore(100,'\n');
 44 
 45     int len2 = strlen(declare_m); //capitalize major even strings
 46     for (int i = 1; i < len2; ++i)
 47     {
 48         if( declare_m[i]  == ' ')
 49            declare_m[i+1] = toupper(declare_m[i+1]);
 50     }
 51 
 52    if (strcmp(declare_m,"Computer Science" ) == 0)
 53         cout << "Your major is " << declare_m << " fellow nerd!!" << endl;
 54    else 
 55      cout << "Your major is: " << declare_m  << endl << endl;
 56      
 57  //  char response = toupper(response);
 58  //  cout << "Is this your major? (Y/N) " << endl;
 59    cin >> response;
 60    cin.ignore(100, '\n');
 61    
 62 } while ('N' == response);
 63 
 64 return 0;
 65 }
I think you're a little confused as to how toupper works. That function takes a char and converts it to the uppercase equivalent. Ie. response = toupper(response); will take whatever is the current value in the response variable, convert it to uppercase, then store that new value into response. It only operates once when you call it, and using it on your variable initialization will do nothing except perhaps give you uppercase junk as a starting value.

On line 59, you read in the char from the user, but don't convert it to uppercase before checking it. You need to add toupper after that line somewhere and before the check in your while loop.
Thanks! I figured it out a little later with help, but this helps too!
Topic archived. No new replies allowed.