Server Help

Trash Talk - help me with c++

Anonymous - Sat Oct 06, 2007 8:39 pm
Post subject: help me with c++
im just learning c++, and working on my small project minicalculator, for some reason when i add any two numbers i get answer like 121545123157 instead of a real answer which is 10 or 20. If you can find any errors in my code il be apreciated

Code: Show/Hide
/*my first program after not pragraming for a year
ShellExecute(NULL, "open", "http://a3studio.4t.com", NULL, NULL, SW_SHOWNORMAL); this command brings u to this website
*/

#include <iostream>
#include <stdlib.h>
#include <windows.h>
using namespace std;

#define PI 3.14 //defines pi so pie now = to 3.14

int main()
{
    SetConsoleTitle("Mini Calculator");  // title
    SetCursorPos(0,0);                    //cursor position
   
    HANDLE text;                              //handle variable
    text = GetStdHandle(STD_OUTPUT_HANDLE);     //make it that
    SetConsoleTextAttribute(text,FOREGROUND_GREEN); //text attribute
   
    menu:
   
    int choice;
    cout<<"Welcome to MiniCalculator \n\n";
    cout<<"Type in 1 and press enter to add"<<endl;
    cout<<"Type in 2 and press enter to subtract"<<endl;
    cout<<"Type in 3 and press enter to multiply"<<endl;
    cout<<"Type in 4 and press enter to divide"<<endl;
    cout<<"Type in 5 and press enter to find area/perimiter of a circle"<<endl;
    cout<<"Type in 6 and press enter to go to google.com"<<endl;
    cout<<"Type in 7 and press enter to exit"<<endl;
    cout<<"Type in your choice and press enter: ";
    cin>>choice;
   
    switch (choice){
           case 1:
                int a;
                int b;
                int c;
               
                c = a + b;
               
                cout<<"\n\n Type in and press enter the number you want to add to: ";
                cin>>a;
                cout<<"\n\n Now type in and press enter the number you want to add: ";
                cin>>b;
                cout<<"The Result is "<<c<<".\n\n\n\n";
                system("pause");
                cout<<"n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
                goto menu;
                }
               
               
                return 0;
                }
               
               
   
    [quote][/quote]

Anonymous - Sat Oct 06, 2007 8:46 pm
Post subject:
oh yea its not finished so dont say i missed out the rest 2-7 choices
Bak - Sat Oct 06, 2007 10:24 pm
Post subject:
c++ programs are executed in order, one line at a time. So when you do "c = a + b" it takes whatever is in a and b at that point in the program and stores in in c.

so doing something like

Code: Show/Hide
c = 5;
c=10;
cout << c;


is perfectly legal and would print out 10. The program first puts 5 into c, then 10, then prints out c, which is 10.
baseball43v3r - Sun Oct 07, 2007 12:56 am
Post subject:
basically what he is saying is that you have to move this line:

c = a + b;

after you input a and b. and then you output c.
Bak - Sun Oct 07, 2007 1:51 am
Post subject:
NEVERMIND I GOT CONFUSED
tcsoccerman - Sun Oct 07, 2007 1:41 pm
Post subject:
Quote:
cout<<"n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";


lol
All times are -5 GMT
View topic
Powered by phpBB 2.0 .0.11 © 2001 phpBB Group