Please Help!!

When I input this code most of the classes come up as Calculus 1. The classes should differ depending on the course number. PLEASE HELP!!

#include "stdafx.h"
#include <iostream>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
char code;
const char *c1 = "CHEM110";
const char *c2 = "MATH241";
const char *c3 = "ENGL101";
const char *c4 = "HIST101";
const char *c5 = "HIST105";
const char *c6 = "OREN104";
const char *c7 = "PHYS205";
const char *c8 = "MATH242";
const char *c9 = "ENGL102";
const char *c10 = "HIST102";
const char *c11 = "HIST106";
const char *c12 = "EEGR105";
const char *c13 = "PHYS206";
const char *c14 = "MATH340";
const char *c15 = "EEGR202";
const char *c16 = "EEGR203";
const char *c17 = "EEGR161";
const char *c18 = "HEED100";
const char *c19 = "MATH243";
const char *c20 = "EEGR221";
const char *c21 = "EEGR215";
const char *c22 = "EEGR211";
const char *c23 = "HUMA201";
const char *c24 = "EEGR305";
const char *c25 = "EEGR322";
const char *c26 = "EEGR317";
const char *c27 = "HUMA202";
const char *c28 = "MATH331";
const char *c29 = "EEGR390";
const char *c30 = "CEGR304";
const char *c31 = "ECON211";
const char *c32 = "EEGR490";
const char *c33 = "EEGR400";
const char *c34 = "IEGR305";
const char *c35 = "BIOL101";
const char *c36 = "EEGR491";
const char *c37 = "HIST350";
const char *c38 = "PHIL109";
const char *c39 = "EEGR243";
const char *c40 = "EEGR409";
const char *c41 = "EEGR412";
const char *c42 = "EEGR417";
const char *c43 = "EEGR419";
const char *c44 = "EEGR424";
const char *c45 = "EEGR431";
const char *c46 = "EEGR440";
const char *c47 = "EEGR443";
const char *c48 = "EEGR444";
const char *c49 = "EEGR451";
const char *c50 = "EEGR453";
const char *c51 = "EEGR454";
const char *c52 = "EEGR460";
const char *c53 = "EEGR461";
const char *c54 = "EEGR462";
const char *c55 = "EEGR463";
const char *c56 = "EEGR465";
const char *c57 = "EEGR471";
const char *c58 = "EEGR481";
const char *c59 = "EEGR487";
const char *c60 = "EEGR489";
const char *c61 = "EEGR498";
const char *c62 = "EEGR499";

cout << "Please type in the course number: ";
cin >> code;

if (code == *c1)
cout << "CHEMISTRY FOR ENGINEERS" << endl;

else if (code == *c2)
cout << "CALCULUS I" << endl;

else if (code == *c3)
cout << "CALCULUS I" << endl;

else if (code == *c4)
cout << "HISTORY I" << endl;

else if (code == *c5)
cout << "HISTORY I" << endl;

else if (code == *c6)
cout << "INTRO TO ENGINEERING I" << endl;

else if (code == *c7)
cout << "PHYSICS I" << endl;

else if (code == *c8)
cout << "CALCULUS II" << endl;

else if (code == *c9)
cout << "FRESHMAN COMPOSITION II" << endl;

else if (code == *c10)
cout << "HISTORY II" << endl;

else if (code == *c11)
cout << "HISTORY II" << endl;

else if (code == *c12)
cout << "INTRO TO ELECT & ENGR" << endl;

else if (code == *c13)
cout << "GENERAL PHYSICS II" << endl;

else if (code == *c14)
cout << "DIFFERENTIAL EQUATIONS" << endl;

else if (code == *c15)
cout << "ELECTRIC CIRCUITS" << endl;

else if (code == *c16)
cout << "INTRO TO ELECTRICAL LAB" << endl;

else if (code == *c17)
cout << "INTRO TO C PROGRAMMING" << endl;

else if (code == *c18)
cout << "HEALTH EDUCATION" << endl;

else if (code == *c19)
cout << "CALCULUS III" << endl;

else if (code == *c20)
cout << "SIGNALS & SYSTEMS" << endl;

else if (code == *c21)
cout << "ELECTRONIC MATERIALS & DEVICES" << endl;

else if (code == *c22)
cout << "INTRO TO DIGITAL LOGIC" << endl;

else if (code == *c23)
cout << "INTRO TO HUMANITIES I" << endl;

else if (code == *c24)
cout << "ELECTROMAGNETICS" << endl;

else if (code == *c25)
cout << "DISCRETE SYSTEMS" << endl;

else if (code == *c26)
cout << "ELECTRONIC CIRCUITS" << endl;

else if (code == *c27)
cout << "INTRO TO HUMANITIES II" << endl;

else if (code == *c28)
cout << "APPLIED PROB & STATS" << endl;

else if (code == *c29)
cout << "PRINCIPLES OF DESIGN" << endl;

else if (code == *c30)
cout << "ENGINEERING MECHANICS" << endl;

else if (code == *c31)
cout << "PRINCIPLES OFECONOMICS" << endl;

else if (code == *c32)
cout << "SR. DESIGN PROJECT I" << endl;

else if (code == *c33)
cout << "INTRO TO PROFESSIONAL PRACTICE" << endl;

else if (code == *c34)
cout << "THERMODYNAMICS" << endl;

else if (code == *c35)
cout << "BIOLOGY" << endl;

else if (code == *c36)
cout << "SR. DESIGN PROJECT II" << endl;

else if (code == *c37)
cout << "INTRO TO AFR DIASPORA" << endl;

else if (code == *c38)
cout << "INTRO TO LOGIC" << endl;

else if (code == *c39)
cout << "This is an elective course" << endl;

else if (code == *c40)
cout << "This is an elective course" << endl;

else if (code == *c41)
cout << "This is an elective course" << endl;

else if (code == *c42)
cout << "This is an elective course" << endl;

else if (code == *c43)
cout << "This is an elective course" << endl;

else if (code == *c44)
cout << "This is an elective course" << endl;

else if (code == *c45)
cout << "This is an elective course" << endl;

else if (code == *c46)
cout << "This is an elective course" << endl;

else if (code == *c47)
cout << "This is an elective course" << endl;

else if (code == *c48)
cout << "This is an elective course" << endl;

else if (code == *c49)
cout << "This is an elective course" << endl;

else if (code == *c50)
cout << "This is an elective course" << endl;

else if (code == *c51)
cout << "This is an elective course" << endl;

else if (code == *c52)
cout << "This is an elective course" << endl;

else if (code == *c53)
cout << "This is an elective course" << endl;

else if (code == *c54)
cout << "This is an elective course" << endl;

else if (code == *c55)
cout << "This is an elective course" << endl;

else if (code == *c56)
cout << "This is an elective course" << endl;

else if (code == *c57)
cout << "This is an elective course" << endl;

else if (code == *c58)
cout << "This is an elective course" << endl;

else if (code == *c59)
cout << "This is an elective course" << endl;

else if (code == *c60)
cout << "This is an elective course" << endl;

else if (code == *c61)
cout << "This is an elective course" << endl;

else if (code == *c62)
cout << "This is an elective course" << endl;

else
cout << "This is not a core requirement " << endl;

cin.get();
system("pause");



return 0;
}

Are you trying to compare a char with a c - string ?
cin.get() will only get one character. If you type in: Chemistry, you'd get C only. Instead of using cin.get(), try a getline. For Example:

1
2
3
4
5
cout << "Please type in the course number: ";

getline( code, cin);     // Gets entire line of input

cout << endl;    // Line spacer before output 


Reference:

http://www.cplusplus.com/reference/string/string/getline/
I believe I am trying to use a string. How do I change the program for that?
Declare code as char* code; to make it a c - string not a single char, then store the input by cin >> code;

and when you compare, you don't need dereference operator, just use the variable name:

A little example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <cstring>

using namespace std;

int main()
{
   const char* myname = "nevermore";
   char* name;
   
   name = "nevermore";
   
   if ( name == myname ) cout << "Names matched !" << endl;
   else cout << "Didn't Matched !" << endl;
   
   return 0;
}
Last edited on
@nevermore28
When you provide suggestions make sure that you know what you're talking about.

Declare code as char* code; to make it a c - string not a single char, then store the input by cin >> code;
this will crash

A little example:
That compares pointer not the strings

@CCoder12
Using std::string would make it indeed much easier if it is allowed:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
string code;

...

cout << "Please type in the course number: ";
cin >> code;

if (code == c1) // Note: no *
cout << "CHEMISTRY FOR ENGINEERS" << endl;

...


return 0;
}
@coder777 i admit i'm wrong with cin but why did my code worked ? i've use an online compiler

and why when i compare name with myname it worked, you said i was comparing pointers

Okay, i promise i will not post any code w/o actually testing it, sorry ...
nevermore28 wrote:
why did my code worked ?
The compiler you used generated the same pointer for the two strings. Other compiler might not.

Use strcmp() to compare (reliably) c strings:

http://www.cplusplus.com/reference/cstring/strcmp/?kw=strcmp
Last edited on
Oh tnx, now i learned another thing: that assumption will not always work in the world of programming
Thank you everyone!

I changed the program to include the strings and it worked perfectly. I did have to take out the stars in front of the c.

The reason it wasn't working was that there was more than one character but it was only reading the first character.

Topic archived. No new replies allowed.