using a switch statement

This is a discussion on using a switch statement within the C++ Programming forums, part of the General Programming Boards category; here is my code. Here is a quick review of what i need to do: open a file of a ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    18

    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[10]={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[0])
    		{
    			case '0' : totals[0]++;
    				break;
    			case '1' : totals[1]++;
    				break;
    			case'2' : totals[2]++;
    				break;
    			case'3' : totals[3]++;
    				break;
    			case'4' : totals[4]++;
    				break;
    			case'5' : totals[5]++;
    				break;
    			case'6' : totals[6]++;
    				break;
    			case'7' : totals[7]++;
    				break;
    			case'8' : totals[8]++;
    				break;
    			case'9' : totals[9]++;
    				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[0])
    		{
    			case '0' : totals[0]++;
    				break;
    			case '1' : totals[1]++;
    				break;
    			case'2' : totals[2]++;
    				break;
    			case'3' : totals[3]++;
    				break;
    			case'4' : totals[4]++;
    				break;
    			case'5' : totals[5]++;
    				break;
    			case'6' : totals[6]++;
    				break;
    			case'7' : totals[7]++;
    				break;
    			case'8' : totals[8]++;
    				break;
    			case'9' : totals[9]++;
    				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. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    		inFile.open("librarybooks-1.txt");
    
    		switch(num11[0])
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    int totals[10]={0,0,0,0,0,0,0,0,0,0};
    That's the same as
    Code:
    int totals[10] = {};
    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[0])
    	{
    		case '0' : totals[0]++;
    			break;
    
    	// ...
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    Your whole switch statement can be simplified into a single line:

    Code:
    ++totals[num11[0]-'0'];

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Actually, a better translation would be:
    Code:
    #include <ctype.h>
    if(isdigit(num11[0])) {
        ++totals[num11[0]-'0'];
    }
    since, once num11 is actually read from the file, the file probably contains stuff other than digits . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    18
    Quote Originally Posted by dwks View Post
    Code:
    	if ( number == 1 )
    	{
    		inFile.open("librarybooks-1.txt");
    	}
    	else if ( number == 2 ) {
    		inFile.open("livejournal-1.txt");
    	}
    
    	switch(num11[0])
    	{
    		case '0' : totals[0]++;
    			break;
    
    	// ...
    Quote Originally Posted by h_howee View Post
    Your whole switch statement can be simplified into a single line:

    Code:
    ++totals[num11[0]-'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. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    18
    Quote Originally Posted by dwks View Post
    Actually, a better translation would be:
    Code:
    #include <ctype.h>
    if(isdigit(num11[0])) {
        ++totals[num11[0]-'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

    Quote Originally Posted by dwks View Post
    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. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Jan 2008
    Posts
    18
    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. #11
    Registered User
    Join Date
    Dec 2004
    Posts
    32
    Quote Originally Posted by dukebdx12 View Post
    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. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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";
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutli Switch statement help
    By elwad in forum C Programming
    Replies: 9
    Last Post: 05-09-2009, 03:19 AM
  2. Switch statement / default:
    By kcpilot in forum C Programming
    Replies: 4
    Last Post: 12-02-2008, 02:14 PM
  3. switch statement
    By guillermoh in forum C Programming
    Replies: 5
    Last Post: 03-10-2008, 02:17 PM
  4. char switch statement
    By jmarsh56 in forum C++ Programming
    Replies: 7
    Last Post: 05-03-2006, 05:04 PM
  5. Efficiency with the switch() statement...
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2001, 02:47 PM

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