Thread: Whats wrong with this program!?

  1. #1
    Unregistered
    Guest

    Whats wrong with this program!?

    Code:
    #include <iostream.h>
    int main()
    {
       int a,b,c;
       cout<<"Welcome to Adam Petrosh's kinda kool kalkulator! Enter 1 for subtraction, 2 for addition, 3 for multiplying, and 4 for division: ";
       cin>>a;
       if(a=1)
       {
          cout<<"You have chosen Subtraction!";
          cout<<"Please enter the two numbers you want to subtract(seperate them with a space): ";
          cin>>b-c;
          cout<<"Prossessing.... complete!";
          cout<<"The answer is ";
          cout<<(b-c);
          cin.get();
       }
       if(a=2)
       {
          cout<<"You have chosen addition!";
          cout<<"Please enter the two numbers you want to add(seperate them with a space): ";
          cin>>b,c;
          cout<<"Prossessing... complete!";
          cout<<"The answer is ";
          cout<<(b+c);
          cin.get();
       }
       if(a=3)
       {
          cout<<"You have chosen multiplication!";
          cout<<"Please enter the two numbers you want to multiply(seperate them with a space): ";
          cin>>b,c;
          cout<<"Prossessing... complete!";
          cout<<"The answer is ";
          cout<<(b*c);
          cin.get();
       }
       if(a=4)
       {
          cout<<"You have chosen division!";
          cout<<"Please enter the two numbers you want to multiply(seperafe them with a space): ";
          cin>>b,c;
          cout<<"Prossessing... complete!";
          cout<<"The answer is ";
          cout<<(b/c);
          cin.get();
       }
       else
       }
          cout<<"I SAID 1, 2, 3, OR 4 YOU IDIOT! PROGRAM TERMINATING!";
          cin.get();
       }
       cin.get();
       return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    223
    The most obvious mistake is using the assignment operator '=' instead of the equality operator '=='

    if(a=1)
    if(a=2)
    ..

    it should be (a == 1) , if(a==2)...etc

    better yet
    if(1 == a)
    if(2 == a)

    .....
    zMan

  3. #3
    Unregistered
    Guest
    ::feels stupid:: thanks...

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Re: Whats wrong with this program!?

    Originally posted by Unregistered
    Code:
    cout<<"I SAID 1, 2, 3, OR 4 YOU IDIOT! PROGRAM TERMINATING!";
    A little tip, instead of barking at the user for printing the wrong number, make a loop so he has the chance of typing it again...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Unregistered
    Guest

    Angry

    DARN! Theres still 3 errors... :-(

  6. #6
    Unregistered
    Guest
    eheh... what user? think about it... besides me... what user will be barked at? besides, I dont know how to do loops. I just got up to if statements on the tutorials...

  7. #7
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    If they are compile-time errors, what lines?

    line 11 is incorrect,

    cin >> a-b // should be cin >> a, b

    On that else block, if you really wanna be that rude to your users, you should at least send it to cerr, because it is notification of invalid output.

  8. #8
    Unregistered
    Guest
    uhm... compile this, run it, then tell me what happens...
    Code:
    #include <iostream>
    int main()
    {
       int a,b,c;
       cout<<"Welcome to Adam Petrosh's kinda kool kalkulator! Enter 1 for subtraction, 2 for addition, 3 for multiplying, and 4 for division: ";
       cin>>a;
       if(a == 1)
       {
          cout<<"You have chosen Subtraction!";
          cout<<"Please enter the two numbers you want to subtract(seperate them with a space): ";
          cin>>b,c;
          cout<<"Prossessing.... complete!";
          cout<<"The answer is ";
          cout<<(b-c);
          cin.get();
       }
       if(a == 2)
       {
          cout<<"You have chosen addition!";
          cout<<"Please enter the two numbers you want to add(seperate them with a space): ";
          cin>>b,c;
          cout<<"Prossessing... complete!";
          cout<<"The answer is ";
          cout<<(b+c);
          cin.get();
       }
       if(a == 3)
       {
          cout<<"You have chosen multiplication!";
          cout<<"Please enter the two numbers you want to multiply(seperate them with a space): ";
          cin>>b,c;
          cout<<"Prossessing... complete!";
          cout<<"The answer is ";
          cout<<(b*c);
          cin.get();
       }
       if(a == 4)
       {
          cout<<"You have chosen division!";
          cout<<"Please enter the two numbers you want to multiply(seperafe them with a space): ";
          cin>>b,c;
          cout<<"Prossessing... complete!";
          cout<<"The answer is ";
          cout<<(b/c);
          cin.get();
       }
       else
       {
          cout<<"I SAID 1, 2, 3, OR 4 YOU IDIOT! PROGRAM TERMINATING!";
          cin.get();
       }
       cin.get();
       return 0;
    }

  9. #9
    Registered User Sekti's Avatar
    Join Date
    Feb 2002
    Posts
    163

    with the if's

    do
    Code:
    if(a == 1)
    {
    cout<<"hi"<<endl;
    }
    else if(a == 2)
    {
    cout<<"Hello"<<endl;
    }
    else if(a == 3)
    {
    cout<<"Howdy"<<endl;
    }
    else if(a == 4)
    {
    cout<<"Hey"<<endl;
    else
    {
    cout<<"1 2 3 or 4 you idiot..."<<endl;
    return 0;
    }
    that way anything besides 1 2 3 4 will display the idiot message and it flows with the else if's
    +++
    ++
    + Sekti
    ++
    +++

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    looks like it will show I SAID 1, 2, 3, OR 4 YOU IDIOT! PROGRAM TERMINATING! after the output for 1, 2, and 3. The else relates to the if(a == 4).

  11. #11
    Unregistered
    Guest
    Like this?
    Code:
    #include <iostream>
    int main()
    {
       int a,b,c;
       cout<<"Welcome to Adam Petrosh's kinda kool kalkulator! Enter 1 for subtraction, 2 for addition, 3 for multiplying, and 4 for division: ";
       cin>>a;
       if(a == 1)
       {
          cout<<"You have chosen Subtraction!"<<endl;
          cout<<"Please enter the two numbers you want to subtract(seperate them with a space): "<<endl;
          cin>>b,c;
          cout<<"Prossessing.... complete!"<<endl;
          cout<<"The answer is "<<endl;
          cout<<(b-c)<<endl;
          cin.get();
       }
       else if(a == 2)
       {
          cout<<"You have chosen addition!"<<endl;
          cout<<"Please enter the two numbers you want to add(seperate them with a space): "<<endl;
          cin>>b,c;
          cout<<"Prossessing... complete!"<<endl;
          cout<<"The answer is "<<endl;
          cout<<(b+c)<<endl;
          cin.get();
       }
       else if(a == 3)
       {
          cout<<"You have chosen multiplication!"<<endl;
          cout<<"Please enter the two numbers you want to multiply(seperate them with a space): "<<endl;
          cin>>b,c;
          cout<<"Prossessing... complete!"<<endl;
          cout<<"The answer is "<<endl;
          cout<<(b*c)<<endl;
          cin.get();
       }
       else if(a == 4)
       {
          cout<<"You have chosen division!"<<endl;
          cout<<"Please enter the two numbers you want to multiply(seperafe them with a space): "<<endl;
          cin>>b,c;
          cout<<"Prossessing... complete!"<<endl;
          cout<<"The answer is "<<endl;
          cout<<(b/c)<<endl;
          cin.get();
       }
       else
       {
          cout<<"I SAID 1, 2, 3, OR 4 YOU IDIOT! PROGRAM TERMINATING!"<<endl;
          cin.get();
       }
       cin.get();
       return 0;
    }

  12. #12
    Unregistered
    Guest
    tch... I cant even read what it says after i put the stuff in... the cin.get() didn't work... The screen still flashes... so i dunno if it works... oh well... anyone got any better ideas for a kinda kool kalkulator?

  13. #13
    Registered User
    Join Date
    Dec 2001
    Posts
    206

    Talking heh

    I know bios will say this anyways to ill say it first: USE SWITCH!


    Code:
    switch(a)
    {
    case '1':
    {    
          cout<<"You have chosen Subtraction!";
          cout<<"Please enter the two numbers you want to subtract(seperate them with 
          a  space): ";
          cin>>b,c;
          cout<<"Prossessing.... complete!";
          cout<<"The answer is ";
          cout<<(b-c);
          cin.get();
          break;
    };
    case '2':
    
       {
          cout<<"You have chosen addition!";
          cout<<"Please enter the two numbers you want to add(seperate them with 
          a space): ";
          cin>>b,c;
          cout<<"Prossessing... complete!";
          cout<<"The answer is ";
          cout<<(b+c);
          cin.get();
          break;
       };
    case '3':
    {
          cout<<"You have chosen multiplication!";
          cout<<"Please enter the two numbers you want to multiply(seperate them with a
          space): ";
          cin>>b,c;
          cout<<"Prossessing... complete!";
          cout<<"The answer is ";
          cout<<(b*c);
          cin.get();
          break;
    };
    case '4':
    
    {
          cout<<"You have chosen division!";
          cout<<"Please enter the two numbers you want to multiply(seperafe them with a 
          space): ";
          cin>>b,c;
          cout<<"Prossessing... complete!";
          cout<<"The answer is ";
          cout<<(b/c);
          cin.get();
          break;
       };
    default:
    
       {
          cout<<"I SAID 1, 2, 3, OR 4 YOU IDIOT! PROGRAM TERMINATING!";
          cin.get();
          break;
       };
    };

  14. #14
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    Originally posted by Unregistered
    Like this?
    yup
    try doing it with a switch statement when you learn about them

  15. #15
    Unregistered
    Guest
    now it just says "I SAID 1, 2, 3, OR 4 YOU IDIOT! PROGRAM TERMINATING!", unless i did it wrong... did I?
    Code:
    #include <iostream>
    int main()
    {
       int a,b,c;
       cout<<"Welcome to Adam Petrosh's kinda kool kalkulator! Enter 1 for subtraction, 2 for addition, 3 for multiplying, and 4 for division: ";
       cin>>a;
       switch(a)
    {
    case '1':
    {    
          cout<<"You have chosen Subtraction!";
          cout<<"Please enter the two numbers you want to subtract(seperate them with 
          a  space): ";
          cin>>b,c;
          cout<<"Prossessing.... complete!";
          cout<<"The answer is ";
          cout<<(b-c);
          cin.get();
          break;
    };
    case '2':
    
       {
          cout<<"You have chosen addition!";
          cout<<"Please enter the two numbers you want to add(seperate them with 
          a space): ";
          cin>>b,c;
          cout<<"Prossessing... complete!";
          cout<<"The answer is ";
          cout<<(b+c);
          cin.get();
          break;
       };
    case '3':
    {
          cout<<"You have chosen multiplication!";
          cout<<"Please enter the two numbers you want to multiply(seperate them with a
          space): ";
          cin>>b,c;
          cout<<"Prossessing... complete!";
          cout<<"The answer is ";
          cout<<(b*c);
          cin.get();
          break;
    };
    case '4':
    
    {
          cout<<"You have chosen division!";
          cout<<"Please enter the two numbers you want to multiply(seperafe them with a 
          space): ";
          cin>>b,c;
          cout<<"Prossessing... complete!";
          cout<<"The answer is ";
          cout<<(b/c);
          cin.get();
          break;
       };
    default:
    
    {
          cout<<"I SAID 1, 2, 3, OR 4 YOU IDIOT! PROGRAM TERMINATING!";
          cin.get();
          break;
       };
    };
       cin.get();
       return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Maze Program - What am I doing wrong?
    By Gipionocheiyort in forum C++ Programming
    Replies: 20
    Last Post: 08-02-2007, 01:31 PM
  2. Replies: 5
    Last Post: 01-13-2007, 02:14 AM
  3. What is wrong with my code? My first program......
    By coreyt1111 in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2006, 02:03 PM
  4. Whats wrong with this program - Output of a series
    By duffmckagan in forum C Programming
    Replies: 2
    Last Post: 07-26-2006, 09:57 AM
  5. Whats wrong with my program?
    By Ruflano in forum C++ Programming
    Replies: 5
    Last Post: 02-21-2002, 05:09 PM