When switch"ing" (....)

If the user inputs 1, would you just use

case 1: or case '1':

thnks

Recommended Answers

All 7 Replies

1 is a number.

'1' is a character.

So, it depends on which of the two you are going to use.

Thanks a lot Jobe, here is my code i am currently working on as of about 10 minutes ago.

#include <iostream>
#include <fstream>


using namespace std;

//SetConsoleTitle("ItemIzer");

int main()
{
    char cho
    
    cout<<"\n\n\n\n\t\t";
    cout<<"1: View Catagory";
    cout<<"2: Append new item";
    cout<<"3: Truncate an item";
    cout<<"4: Create Catagory";
    cout<<"5: Delete Catagory";
    
    do
    {
              bool repeat(false);
              cout<<">>";
              cin>>cho;
              
              switch (cho)
              {
                     case 1:
                     case 2:
                     case 3:
                     case 4:
                          void name;
                          ofstream Clist ("Catagorylist.txt", app);
                          cin>>name;
                          Clist<<name;
                     case 5:

Would everything in there be legal so far as long as i finishing declaring everything? Thanks

Since your switch variable cho is a char, your case labels must be like case '1': - the labels must be literal characters.

- char cho ;

- After each case you have to use a break; , otherwise, the program will fall threw to each case that is presented.

- I guess you haven't shown all the code, seeing as you didn't use close braces for the do loop and main.

Yes i understand i need to use break; =P

So what if i make cho an integer? Then it would be 1 and not '1' correct?

>> - the labels must be literal characters.

I got a long way to go lol. Thanks in advance

Yes i understand i need to use break; =P

So what if i make cho an integer? Then it would be 1 and not '1' correct?

>> - the labels must be literal characters.

I got a long way to go lol. Thanks in advance

Yep, like vmanes mentioned !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.