string array, if statement, and comparisons

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2007
Posts: 47
Reputation: teppuus is an unknown quantity at this point 
Solved Threads: 0
teppuus's Avatar
teppuus teppuus is offline Offline
Light Poster

string array, if statement, and comparisons

 
0
  #1
Sep 25th, 2007
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:

  1.  
  2. if (studentId[3] == 1)
  3. cout << "Freshman ";
  4.  
  5. else if (studentId[3] == 2)
  6. cout << "Sophmore ";
  7.  
  8. else if (studentId[3] == 3)
  9. cout << "Junior ";
  10.  
  11. else if (studentId[3] == 4)
  12. 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!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,697
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 728
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: string array, if statement, and comparisons

 
0
  #2
Sep 25th, 2007
If you want the character representation of a digit, wrap it in single quotes:
  1. if (studentId[3] == '1')
  2. cout << "Freshman ";
  3.  
  4. else if (studentId[3] == '2')
  5. cout << "Sophmore ";
  6.  
  7. else if (studentId[3] == '3')
  8. cout << "Junior ";
  9.  
  10. else if (studentId[3] == '4')
  11. cout << "Senior ";
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 47
Reputation: teppuus is an unknown quantity at this point 
Solved Threads: 0
teppuus's Avatar
teppuus teppuus is offline Offline
Light Poster

Re: string array, if statement, and comparisons

 
0
  #3
Sep 25th, 2007
Thank you. That did the trick. I didn't know that all components in a string are characters, even if they are numbers.....which makes since. That helped me solve a problem I was having during error checking as well. Thank you! It is always great to learn something new.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC