Thread: Bug in Code

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    13

    Bug in Code

    Code:
    void main(int argc, char **argv[]) { 
    	int equals1 = 0;
    	int equals2 = 0;
    	int equals3 = 0;
    
    	cout << "Enter a list of integers, that terminates with a letter (e.g. 1 2 3 4 k)" << endl;
    	int value;
    	while (cin >> value){
    		switch (value) {
    			case 1: equals1++; 
    			case 2: equals2++; 
    			case 3: equals3++; 
    		}
    	}
    	
    	char endOfInput;
    	cin >> endOfInput;
    
    	cout << equals1 << " inputs equals 1" << endl;
    	cout << equals2 << " inputs equals 2" << endl;
    	cout << equals3 << " inputs equals 3" << endl;
    
    	int keypress; cin >> keypress;
    }
    This code doesnt work, when i debug it breaks down on the int equals1 = 0 line....anyone know why??

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    >> void main(int argc, char **argv[])
    this is wrong

    int main(int argc, char *argv[])
    this isnt wrong

    int main(int argc, char **argv)
    this isnt really wrong either, the standard might specify which is correct... you might want to have a looksy and see.

    edit: doh! copied and pasted void... ugh, i feel dirty now.
    Last edited by Perspective; 10-20-2004 at 11:26 AM.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    113
    first try:
    #include<iostream>
    usind std::cout;
    using std::cin;
    using std::endl;

    then:
    int main(int argc, char **argv[])
    {
    ....

    return 0;
    }

    shoud run fine now

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Borth wrong,
    Code:
    int main(int argc, char **argv[])  // not void and not **argv[], either **argv, or *argv[]
    {
        ....
    
        return 0;
    }
    Edit: echo perspectives edit.
    Last edited by Shakti; 10-20-2004 at 11:55 AM.

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    Not one of you has presented a correct version of a main definition in code...

    Code:
    int main( int argc, char* argv[] ) //or char** argv
    {
         //...
        // return 0; is implicit
    }

  6. #6
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    And just for reference in case someone reads this and doesn't know which of the four responses is correct (its PorkyChop):

    http://www.research.att.com/~bs/bs_faq2.html#void-main

  7. #7
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Quote Originally Posted by PorkyChop
    Not one of you has presented a correct version of a main definition in code...
    ack, ive been defeated by the copy/paste pitfall. see my edited post for feelings on the subject

  8. #8
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Just a note. Even though the 'return 0;' at the end of main (and only main) is implicit according to the standard (assuming you didn't say anything else), some compilers may scream at you about this... So just give them the 'return 0;' to make them happy.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  9. #9
    Registered User
    Join Date
    Oct 2004
    Posts
    13
    Thanks for your help got it working, another point to note is that i missed the break statement at the end of each case so when a number 1 occured it also added to 2 and 3

  10. #10
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    To simplify your code a bit, you can use the simpler version of main (since you do not make use of the cmd line args). Here are the valid forms of main:
    Code:
    int main( )
    int main(int argc, char** argv) // or char* argv[]
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. CODE Tags script bug
    By Mario F. in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 10-15-2006, 02:18 PM
  2. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  3. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  4. Replies: 0
    Last Post: 02-21-2002, 06:05 PM
  5. Debugging leads to buggy code and longer hours?
    By no-one in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 01-28-2002, 11:14 AM