# Thread: using a switch statement

1. ## using a switch statement

here is my code. Here is a quick review of what i need to do: open a file of a list of numbers and get the first number and tally it for that number. Ex. number is 4563 it tallys 1 for number 4. And 2983 it tallys 1 for number 2. and loops through whole list and tallys numbers 1 - 9 and records them in the output. This outputs what i say but it outputs 0's for 1-9 instead of how many times it found a number that started with 1 and etc through 9.

Code:
```#include <fstream>
#include <iostream>
#include <string>

using namespace std;

int getFirstDigit(int num);

int main ()
{

ifstream inFile;
int number;
int num1=0, num2=0, num3=0, num4=0, num5=0, num6=0, num7=0, num8=0, num9=0;
string num11;
int totals={0,0,0,0,0,0,0,0,0,0};
cout << "Enter 1 for librarybooks or Enter 2 for livejournal : ";
cin >> number;

if ( number == 1 )
{
inFile.open("librarybooks-1.txt");

switch(num11)
{
case '0' : totals++;
break;
case '1' : totals++;
break;
case'2' : totals++;
break;
case'3' : totals++;
break;
case'4' : totals++;
break;
case'5' : totals++;
break;
case'6' : totals++;
break;
case'7' : totals++;
break;
case'8' : totals++;
break;
case'9' : totals++;
break;
}

for (int i=1; i < 10; ++i)
{
cout << "Number of " << i << "s : " << totals[i] << endl;
}
}

else if ( number == 2 )
{

inFile.open("livejournal-1.txt");

switch(num11)
{
case '0' : totals++;
break;
case '1' : totals++;
break;
case'2' : totals++;
break;
case'3' : totals++;
break;
case'4' : totals++;
break;
case'5' : totals++;
break;
case'6' : totals++;
break;
case'7' : totals++;
break;
case'8' : totals++;
break;
case'9' : totals++;
break;
}

for (int i=1; i < 10; ++i)
{
cout << "Number of " << i << "s : " << totals[i] << endl;
}
}

else
cout << "\n" << "Invalid number" << "\n" << endl;

return 0;
}``` 2. Code:
```		inFile.open("librarybooks-1.txt");

switch(num11)```
you are lucky that this does not crashed
you are opening the file, but where do you read info from it? your num11 string is always empty 3. Code:
`int totals={0,0,0,0,0,0,0,0,0,0};`
That's the same as
Code:
`int totals = {};`
because any non-specified elements get initialized to zero. (In C, you need to use {0}, but in C++, {} works.)

Code:
`for (int i=1; i < 10; ++i)`
I think you want to start at 0, not 1.

Code:
`case'7' :`
Unusual spacing, to be sure. Since your code for number==1 and number==2 is the same except for which file you open, why not wrap only the file opening code in an if/else construct, and have everything else right in main()?
Code:
```	if ( number == 1 )
{
inFile.open("librarybooks-1.txt");
}
else if ( number == 2 ) {
inFile.open("livejournal-1.txt");
}

switch(num11)
{
case '0' : totals++;
break;

// ...``` 4. Your whole switch statement can be simplified into a single line:

Code:
`++totals[num11-'0'];` 5. Actually, a better translation would be:
Code:
```#include <ctype.h>
if(isdigit(num11)) {
++totals[num11-'0'];
}```
since, once num11 is actually read from the file, the file probably contains stuff other than digits . . . . 6. Originally Posted by dwks Code:
```	if ( number == 1 )
{
inFile.open("librarybooks-1.txt");
}
else if ( number == 2 ) {
inFile.open("livejournal-1.txt");
}

switch(num11)
{
case '0' : totals++;
break;

// ...``` Originally Posted by h_howee Your whole switch statement can be simplified into a single line:

Code:
`++totals[num11-'0'];`
thanks. I have used both those ideas which has shortened my code. My output for each number is still 0 for each number 1-9 instead of the tallied number it came up with. 7. Well, do you have something like this?
Code:
`inFile >> num11;`
for a word, or
Code:
`getline(inFile, num11);`
for a whole line.

As it stands, your num11 is never set. 8. Originally Posted by dwks Actually, a better translation would be:
Code:
```#include <ctype.h>
if(isdigit(num11)) {
++totals[num11-'0'];
}```
since, once num11 is actually read from the file, the file probably contains stuff other than digits . . . .
yes it does...first 2 lines are words Originally Posted by dwks Well, do you have something like this?
Code:
`inFile >> num11;`
for a word, or
Code:
`getline(inFile, num11);`
for a whole line.

As it stands, your num11 is never set.
im new to this...how do i set it? also, since i am using a switch someone had helped me previously with this and used an array. i think i might have some stuff in there containing both but i need to get rid of and just use a switch if this makes any sense. 9. What dwks means is that you define the std::string num11, but you never use it, so it's going to end up empty no matter what you do. 10. yea i had to code working perfect until i realized I had to put something in to ask the user to pick out of 2 files for this program to loop through. For that i used an if else statement and now i can't seem to get it to work. It just outputs 0's instead of the numbers like it did when i had it. 11. Originally Posted by dukebdx12 i used an if else statement and now i can't seem to get it to work. It just outputs 0's instead of the numbers like it did when i had it.
can you post your updated code? 12. You're doing something like this.
Code:
```string str;
ifstream file("whatever.txt");

if(str == "") {
cout << "why is str empty???\n";
}```
Opening the file is a completely independent operation from looking at the contents of the string. Opening a file does not magically "tie" a string to the file, thus putting the contents of the file in the string. You have to do that yourself.
Code:
```string str;
ifstream file("whatever.txt");

file >> str;

if(str != "") {
cout << "Aha! The string is no longer empty . . . .\n";
}``` Popular pages Recent additions 