473,403 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Problems with my first switch statement.

I am new to C++, and using gcc for a compiler. This is my first switch statement I have written, but it will not compile, and I can't figure out why (I have stared at it for a while now). The errors messages say there are syntax errors on line 15, and 35, but I can't find them! Sorry about the obvious question.
Thanks for your help.

here is the code

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. char intro(char ans);
  6. //Requires: The user must input one of the following a char values:
  7. //q,Q,m,M,i,I.
  8. //Promises: Returns the input to main.
  9.  
  10. int main()
  11. {
  12.   char ans= intro(char ans);
  13.   do
  14.     {
  15.       switch (ans)
  16.         {
  17.         case 'q':
  18.         case 'Q':
  19.           cout<<"Q"<<endl;
  20.           break;
  21.         case 'm':
  22.         case 'M':
  23.           cout<<"M"<<endl;
  24.           break;
  25.         case 'i':
  26.         case 'I':
  27.           cout<<"I"<<endl;
  28.           break;
  29.         default:cout<<"improper data"<<endl;
  30.           //intro runs good data.
  31.         }
  32.     }while (ans!=="q"||ans!=="Q"||ans!=="m"||ans!=="M"||ans!=="i"||ans!=="I");
  33.  
  34. }
  35.  
  36. char intro(char ans)
  37. {
  38.   cout<<"What the program does, and select one of the 3 options (m,i,q)"<<endl;
  39.   cin>> ans;
  40.   return ans;
  41. }
  42.  
  43.  
Mar 1 '08 #1
1 1190
oler1s
671 Expert 512MB
The errors messages say there are syntax errors on line 15, and 35,
Here's a hint. It's very useful to tell us what those syntax errors are, as we don't have an inbuilt compiler in our heads...

I see a mistake in ans!=="q" (in the while loop) and the similarly written not-equal comparisons. For one, the not equal operator is !=, not !==. Next, there is a difference in using ' ' and " " (single quotes vs. double quotes). As a more knowledgeable C++ programmer, you will eventually understand the implications of using a "".

For now, understand this. Whenever you have a single character, it should be surrounded by '. Never by ".
Mar 1 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Mark Morton | last post by:
I'm writing an if statement for a UK credit card form validation script. Users who specify that their card is Switch need to enter either the issue number or the 'valid from' date. I'm trying to...
13
by: Yodai | last post by:
Hi all.... I have a little problem that's driving me nuts. I can't seem to make any sense of it. I have this small webserver that substitutes some data from a page when finds a substitution...
17
by: Lloyd Sheen | last post by:
This IDE is driving me nuts. I needed another button so I copied an existing one, changed the Text and the id and position by drag and drop. Well then I run and get the following: Control...
4
by: marti | last post by:
I've run into three problems trying to solve this one issue. Using VS2005 B2 & sql server 2000, I create a sqldatasource and bind a gridview to it. Everything looks good. However, if there is an...
14
by: Wescotte | last post by:
I have an application that uses several file formats for similar data. So I've created various php files for each format containing the same functions which produce the same end result. Now I...
9
by: mantrid | last post by:
hello In the function below radtext is an array of 3 radio buttons with values set to 1, 2 and 3. but variables starty and finishy are not returned. ...
12
by: | last post by:
Is it fine to call another method from Switch? Eg. Switch (stringVar) { case ("a"): somVar = "whatever"; Another_Method(); //call another method return;
4
by: huzzaa | last post by:
I am using a switch statement that will decide from a random number what message to display. I want the random number to be between 0 and 100 and then if the number is say between 1 and 10 to...
9
by: yndygo | last post by:
Alright then - I know it's got to be out there somewhere, but I can't find it... I'm looking at a piece of code and there's some debate as to what the behavior will be. Given a switch/case...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.