Switch Statement Help

This is a discussion on Switch Statement Help within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> using namespace std; int main() { char num; cout << "Type in the interger you wish to ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    14

    Switch Statement Help

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    
    char num;
    
    cout << "Type in the interger you wish to convert: ";
    cin >> num;
    
    switch(num)
    {
    case '1' : cout << "one" << endl;
    case '2' : cout << "two" << endl;
    case '3' : cout << "three" << endl;
    case '4' : cout << "four" << endl;
    case '5' : cout << "five" << endl;
    case '6' : cout << "six" << endl;
    case '7' : cout << "seven" << endl;
    case '8' : cout << "eight" << endl;
    case '9' : cout << "nine" << endl;
    case '10': cout << "ten" << endl;
    
    return 0;
    }
    }
    Keeps producing the errors
    Assignment1.cpp:23:6: warning: multi-character character constant
    Assignment1.cpp: In function 'int main()':
    Assignment1.cpp:23: warning: overflow in implicit constant conversion

    any hints/help with this? im still brand new to c++ and am working through a book.

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    A couple of issues here:
    • num is defined as a char, which can only hold one char info; so 10 would be stored as 1.
    • Since you do not break out of your switch statements, everything past the entry point will be printed.
    • Your indentation needs dramatic improvement, which would have lead you to see that your return statement is inside of your switch statement.

    Something like this is what I believe you are looking for:
    Code:
    #include <iostream>
    
    int main(void){
    
    	int num;
    	std::cout<<"Enter number:";
    	std::cin>>num;
    
    	switch(num){
    		case 1:
    			std::cout<<"One";
    			break;
    		case 2:
    			std::cout<<"Two";
    			break;
    		default:
    			std::cout<<"Not sure";
    	}
    
    	return (0);
    }
    You can find out more information about the switch statement with Lesson 5: Switch case
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    17
    I haven't used switch-case statements in a while (because my classes decided to drag me through C and assembly for two semesters), but I'm pretty sure it'd be easier to take your input as an integer, and made cases using said integer.

    Also, as AndrewHunter said, put break statements in there, and practice indentation. Indenting makes your code a lot more readable, especially as your programs get larger.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a switch statement
    By k_smith in forum C Programming
    Replies: 2
    Last Post: 07-12-2011, 11:50 AM
  2. Switch statement
    By beene in forum C++ Programming
    Replies: 21
    Last Post: 07-01-2007, 08:13 AM
  3. After the switch statement?
    By cerin in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2005, 07:01 PM
  4. Switch statement
    By big146 in forum C++ Programming
    Replies: 7
    Last Post: 06-25-2004, 07:16 AM
  5. help with switch statement?
    By Kristina in forum C++ Programming
    Replies: 10
    Last Post: 05-14-2002, 11:25 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21