HELP with line 47-49 of my code?

Hello, I've tried looking into this but I'm not finding a direct answer online.
I'm writing a program that will take in the users name and major. It will capitalize the first letter of every new string and it will return the value to the user. I need help with adding a statement. I'm trying to return a fun phrase only if the user enters 'computer science' or 'Computer Science.' I would like to know if I even need to include an or statement since it's being converted to upper case anyway. I tried using strcmp,but from my understanding, that is used to compare two string values. I'm not really seeing a reason I need to compare anything. Final question, is the code not working because if statements can't read multiword strings? Wait as I write this I might be getting an idea..but I'll still ask. Should I be using string compare with my declare_m variable and "computer science?" Thank you sooooo much!!

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
  2 // The purpose of this program is to make a C++ Program that uses arrays
  3 
  4  #include <iostream>
  5 #include <cctype>
  6 #include <cstring>
  7 
  8 using namespace std;
  9 
 10 const int  NAME = 21;
 11 const int  MAJOR = 45;
 12 
 13 
 14 int main()
 15 {
 16     char person [NAME]; // variables store names of users
 17     char declare_m [MAJOR];
 18 
 19 
 20     cout << "Enter name: " << endl; //gets user names and converts to upper
 21     cin.get(person, NAME, '\n');
 22     cin.ignore(100, '\n');
 23     person[0] = toupper(person[0]);
 24 
 25     int len1 = strlen(person);// capitalizes every word after a statement
 26     for (int i = 1; i < len1; ++i) //starts index 1
 27       {
 28        if(person[i] == ' ')
 29           person[i+1] = toupper(person[i+1]);
 30       }
 31     cout << "Hello " << person << endl;
 32     cout << "Length of word is: " << len1 << endl;
 33 
 34     cout << "What's your major? " << endl; // get user major
 35     cin.get (declare_m, MAJOR,'\n');
 36     declare_m[0] = toupper(declare_m[0]);
 37     cin.ignore(100,'\n');
 38 
 39     int len2 = strlen(declare_m); //capitalize major even strings
 40     for (int i = 1; i < len2; ++i)
 41     {
 42         if( declare_m[i]  == ' ')
 43            declare_m[i+1] = toupper(declare_m[i+1]);
 44     }
 45 
 46 
 47    if (declare_m == "Computer Science" || declare_m == "computer science")
 48         cout << "Your major is " << declare_m << ",fellow nerd!!!! " <<  endl;
 49    else
 50         cout << "Your major is: " << declare_m << endl;
 51 
cs162_lab3.cpp [+]                                                                                                                                                 2,58           Top
-- INSERT --
Last edited on
Try adding brackets to the if and else.
@Bob It's not much important to insert { } in every single if statement.

@SVcpp
Yes, you used C-style string so you have to use strcmp() to compare string.
Because declare_m == "Computer Science" will be always fault, if you know array is pointer that store only address, so if you use == operator, sure that the address of declare_m and "Computer Science" can't be the same.
yes insted of comparing arrays with == use strcmp()
http://www.cplusplus.com/reference/cstring/strcmp/
@SVcpp

To use lsk's idea, here's how to use the strcmp()
Instead of this
1
2
3
4
if (declare_m == "Computer Science" || declare_m == "computer science")
       cout << "Your major is " << declare_m << ",fellow nerd!!!! " <<  endl;
   else
       cout << "Your major is: " << declare_m << endl;


1
2
3
4
5
6
 cout << "Your major is " << declare_m; // No sense writing this twice
 if (strcmp(declare_m ,"Computer Science") == 0 ) // Check if they are the same
	cout <<  ", fellow nerd!!!! ";// And print this only IF they match
 cout << endl << endl; // Add a couple newlines
 return 0;
}
@Bob The Zealot
it will work without brackets.

why dont you use bool and instead bool or
svcpp wrote:
purpose of this program is to make a C++ Program

Then why not do this in the C++ way and use std::string instead of C-style strings?
1
2
3
4
5
6
7
8
9
10
11
#include <string> 

    string person; // variables store name of user
    string declare_m;
...
    getline (cin, person);
    getline (cin, declare_m);
    size_t len1 = person.size();
    size_t len2 = declare_m.size();
...
    if (declare_m == "Computer Science" || declare_m == "computer science")
@AbstractionAnon maybe he's using a book that is using string as default of the author?
so he didnot know about it
THANKS EVERYONE!


I appreciate it. I'm strictly doing the project the way my teacher wants it. I'm new to this and am at the point where I will just do what I'm told basically. I appreciate the help from you all.

why is it set to == 0?

could that be explained?
@SVcpp

A double equals, is used to check if variable one 'IS EQUAL TO' variable two. If you use a single equal, you are assigning a value 'TO' the variable.
Topic archived. No new replies allowed.