Whats wrong with this..

This is a discussion on Whats wrong with this.. within the C++ Programming forums, part of the General Programming Boards category; #include <stdio.h> #include <iostream.h> int main() { int first_number = 0; int second_number = 0; int answer = first_number+=second_number; int ...

  1. #1
    ThirdEye
    Guest

    Whats wrong with this..

    #include <stdio.h>
    #include <iostream.h>

    int main()
    {
    int first_number = 0;
    int second_number = 0;
    int answer = first_number+=second_number;
    int make_one_letter_and_enter_to_exit;
    do
    {

    cout<<"Please enter the first number\n";
    cin>>first_number;

    cout<<"Please enter the second number\n";
    cin>>second_number;

    cout<<"Answer is: "<<answer;<<"\n";
    cin>>answer;

    cout<<"Type anything and press enter to exit";
    cin>>make_one_letter_and_enter_to_exit;
    return 0;
    }
    }

  2. #2
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    please use code tags....

    Code:
    #include <stdio.h>
    #include <iostream.h>
    
    int main()
    {
    	int first_number = 0;
    	int second_number = 0;
    	int answer = first_number+=second_number;
    	int make_one_letter_and_enter_to_exit;
    	do 
    	{
    
    		cout<<"Please enter the first number\n";
    		cin>>first_number;
    
    		cout<<"Please enter the second number\n";
    		cin>>second_number;
    
    		/*suggestion: 
    		  answer = first_number + second_number; 
    		*/
    		cout<<"Answer is: "<<answer<<endl; //note: answer was defined as 0 from start
    		cin>>answer; //why?
    		
    
    		cout<<"Type anything and press enter to exit";
    		cin>>make_one_letter_and_enter_to_exit;
    		return 0;
    	} while(1);
    	return 0;
    }
    First, the do command is used in association with a do-while loop and you must have a loop/loop condition. Hence the while(1);
    Second, why would you use a do-while loop when its going to return before it even loops? In any case just include the while at the end of the do's scope, and remember the ';'

    -LC
    Last edited by Lynux-Penguin; 07-21-2003 at 12:08 PM.
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  3. #3
    ThirdEye
    Guest
    Thanks!!

  4. #4
    Registered User Azmeos's Avatar
    Join Date
    Jun 2003
    Posts
    65
    Code:
    #include <stdio.h>
    #include <iostream.h>
    
    int main()
    {
    	int first_number = 0;
    	int second_number = 0;
    	int answer = 0; //adding 1st and 2nd number here has no effect... since they are both zero.
    	int make_one_letter_and_enter_to_exit;
    	do 
    	{
    
    		cout<<"Please enter the first number\n";
    		cin>>first_number;
    
    		cout<<"Please enter the second number\n";
    		cin>>second_number;
    
    		/*suggestion: 
    		  answer = first_number + second_number; 
    		*/
    		cout<<"Answer is: "<<answer<<endl; //note: answer was defined as 0 from start
    		cin>>answer; //why?
    		
    
    		cout<<"Type anything and press enter to exit";
    		cin>>make_one_letter_and_enter_to_exit;
    		return 0;
    	} while(1);
    	return 0;
    }

  5. #5
    ThirdEye
    Guest

    What about this?

    #include <stdio.h>
    #include <iostream.h>

    int main()
    {
    int first_number = 0;
    int sign;
    int second_number = 0;
    int answer; // answer
    int make_one_letter_and_enter_to_exit; // to make the program stay up afterthe answer is sent
    do
    {
    cout<<"Welcome to my calculator program\n\n"; // welcome message
    cout<<"first number:\n"; // here u write the first number
    cin>>first_number; // saves the first number

    cout<<"Calculate function(+,-,*, or /):\n"; // here you chooce what you gonna calculate the 2 numbers with
    cin>>sign;

    cout<<"second number:\n"; // here u write the second number
    cin>>second_number; // saves the second number

    cout<<"Answer is: "<<first_number sign second_number<<endl; // This is where the problem is.. what I shall use instead of "sign" only
    cin>>answer;

    cout<<"Type anything and press enter to exit"; // to make the prog stay up after the answer is displayed, but it dont
    cin>>make_one_letter_and_enter_to_exit;
    return 0;
    } while(1);
    return 0;
    }

  6. #6
    Registered User Azmeos's Avatar
    Join Date
    Jun 2003
    Posts
    65
    PLEASE use code tags.

    do a SWITCH sentence...

    Code:
    switch(sign) {
    	case '*':
    		answer = first_number * second_number;
    		break;
    	case '+':
    		answer = first_number + second_number;
    		break;
    
    	//etc...
    }
    then just cout the answer.

  7. #7
    Registered User
    Join Date
    Jul 2003
    Posts
    2

    error by using that?

    aha.. Ok sorry, I will in from now on! First time using this forum

    When I added this, it just didnt ask me the for the second_number, but it stopped when it came to the "which sign" question. That is because of the "brake;" or ?
    Also I am very new with c++ so dont laugh if this seems very stupid to you hehe

    PHP Code:
    #include <stdio.h>
    #include <iostream.h>

    int main()
           {
    int first_number 0;
    int sign;
    int second_number 0;
    int answer;  // answer
    int make_one_letter_and_enter_to_exit;  // to make the program stay up afterthe answer is sent

    do
           {
              
    cout<<"Welcome to my calculator program\n\n"
              
    cout<<"first number:\n"// here u write the first number
              
    cin>>first_number;      // saves the first number

              
    cout<<"Calculate function(+,-,*, or /):\n";  
              
    cin>>sign;

                   switch(
    sign) {
                 case 
    '*':
                      
    answer first_number second_number;
                                    break;
                 case 
    '+':
            
    answer first_number second_number;
                                    break;
                    case 
    '-':
            
    answer first_number second_number;
                                    break;
                    case 
    '/':
            
    answer first_number second_number;
                                    break;
                    }



              
    cout<<"second number:\n";                                                 
              
    cin>>second_number;

              
    cout<<"Answer is: "<<answer<<endl
              
    cin>>answer;

              
    cout<<"Type anything and press enter to exit";   
              
    cin>>make_one_letter_and_enter_to_exit;


      return 
    0;
    } while(
    1);
      
      return 
    0;

    Last edited by ThirdEye; 07-22-2003 at 10:45 AM.

  8. #8
    Geo Geo Geo-Fry
    Join Date
    Feb 2003
    Posts
    116
    it doesnt work because sign is declared as an int, yet you want a char. keep everything the same but replace
    Code:
    int sign;
    with
    Code:
    char sign;
    and you will be good to go
    but why do you still have the
    Code:
    cin >> answer;
    at the end?
    "You can lead a man to Congress, but you can't make him think."
    "The Grand Old Duke of York
    -He had ten thousand men.
    -His case comes up next week."
    "Roses are red, violets are blue, I'm schizophrenic, and so am I."
    "A computer once beat me at chess, but it was no match for me at kick boxing."
    "More and more of our imports are coming from overseas."
    --George W. Bush
    "If it weren't for electricity, we'd all be wacthing TV by candlelight."
    --George W. Bush

  9. #9
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Change
    Code:
    int sign;
    to
    Code:
    char sign;
    You should move
    Code:
    cout<<"second number:\n";                                                 
    cin>>second_number;
    before the swith-case otherwise you'll end up with the wrong answer.

    Dont understand why you have that second line there. You showing the user the answer but then you want the user to input some other data and place it into the answer variable. Dont think you need that second line.
    Code:
    cout<<"Answer is: "<<answer<<endl; 
    cin>>answer;
    Your code looks something like this...
    Code:
      do
      {
         // your code..
         cout<<"Type anything and press enter to exit";   
         cin>>make_one_letter_and_enter_to_exit;
    
         return 0;
      } while(1);
      
      return 0;
    A suggestion would be,
    Code:
        char quit;
        do
        {
        
            cout << "Do you want to quit (Y/N) ? ";
            cin >> quit;
        }while ( quit == 'N' || quit == 'n');
        
        return 0;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 09:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

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