| | |
string array, if statement, and comparisons
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
Ok, I know you aren't suppose to (or can't) do comparions on arrays such as, if (x < y), when both x and y are single dimensional arrays. But what about if you have a string and want to compare one element in that string. For example, I am trying to do:
The fourth character in the string is either 1, 2, 3, 4. My program compiles, but it just skips this section. I placed a cout << studentId[3]; statement right before this to see if I am passing the studentId correctly....I am because the program spits out the correct studentId[3] every time. I searched other people's code on this website, and it seems others are writing comparisons like this in their code. Any tips on how I can move further or what I am doing wrong? Thank you!
C++ Syntax (Toggle Plain Text)
if (studentId[3] == 1) cout << "Freshman "; else if (studentId[3] == 2) cout << "Sophmore "; else if (studentId[3] == 3) cout << "Junior "; else if (studentId[3] == 4) cout << "Senior ";
The fourth character in the string is either 1, 2, 3, 4. My program compiles, but it just skips this section. I placed a cout << studentId[3]; statement right before this to see if I am passing the studentId correctly....I am because the program spits out the correct studentId[3] every time. I searched other people's code on this website, and it seems others are writing comparisons like this in their code. Any tips on how I can move further or what I am doing wrong? Thank you!
If you want the character representation of a digit, wrap it in single quotes:
C++ Syntax (Toggle Plain Text)
if (studentId[3] == '1') cout << "Freshman "; else if (studentId[3] == '2') cout << "Sophmore "; else if (studentId[3] == '3') cout << "Junior "; else if (studentId[3] == '4') cout << "Senior ";
I'm here to prove you wrong.
![]() |
Similar Threads
- How do I convert a vector to a String array ? (Java)
- Declaring string array, initializing later... (C++)
- Copy string into an array (C#)
- ArrayList to multi-dimensional string array (C#)
- Geting elements from a String Array (C++)
Other Threads in the C++ Forum
- Previous Thread: algorithm for linked list
- Next Thread: problem with do...while loop
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






