Thread: review code

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    2

    Exclamation review code

    hello, this is my first try of math operation with variables:

    #3include <iostream.h>
    int main()
    {
    // declare variables

    int firstNumber;
    int secondNumber;
    int result = firstNumber + secondNumber;

    //read variables content

    cout<<"please enter 1st number:";
    cin>>firstNumber;
    cout<<"enter 2nd number:";
    cin>>secondNumber;

    // get the result of a + b
    count<< "you get: result";

    //here's my problem, i just can introduce firstnumber valor, then window closes. I use
    cin.get();
    result 0;

    }


    So, how can i mantain the windows open, or maybe let press enter without closing window.

    Thanks.

  2. #2
    Me
    Guest
    First of all, you should remove the "3" from the header file. You probably shouldn't declare the value of result at the beginning of the program. As for the window closing early, try this: add the header file #include<conio.h>
    and use the getch() command at the end of the program. Try a source code like this:

    #include<iostream.h>
    #include<conio.h>
    int main()
    {
    //declare variables
    int firstNumber;
    int secondNumber;
    int result;
    cout<<"please enter 1st number:\n";/*included \n for carriage return. you can remove it if you like*/
    cin>>firstNumber;
    cout<<"enter 2nd number:\n";
    cin>>secondNumber;
    result=firstNumber+secondNumber;
    cout<<"you get: "<<result;
    getch();
    }

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571

    Re: review code

    Originally posted by absenta
    hello, this is my first try of math operation with variables:

    #3include <iostream.h>
    int main()
    {
    // declare variables

    int firstNumber;
    int secondNumber;
    int result = firstNumber + secondNumber;

    //read variables content

    cout<<"please enter 1st number:";
    cin>>firstNumber;
    cout<<"enter 2nd number:";
    cin>>secondNumber;

    // get the result of a + b
    count<< "you get: result";

    //here's my problem, i just can introduce firstnumber valor, then window closes. I use
    cin.get();
    result 0;

    }


    So, how can i mantain the windows open, or maybe let press enter without closing window.

    Thanks.
    Well a real cheesy way to keep the window open is, at the bottom of your code right before result 0 (also this should be return 0) you can put

    system( "pause" );

    Which sends the command 'pause' to DOS. Otherwise you can #include <conio.h> and use getch(); and the program will wait for a key input. Also, when you're initializing result just assign it to 0. Later in the program after you have gotten the user's 2 variables, then say result = firstNumber + secondNumber; Because right now the way you have it, the compiler assigns whatever random thing is in memory for firstNumber and secondNumber to result. So.. perhaps it should look something like this...
    Code:
    #include <iostream>
    using namespace std; // For new streams
    
    int main()
    {
         // declare variables
    
         int firstNumber = 0;
         int secondNumber = 0;
         int result = 0;
    
         //read variables content
    
         cout<<"please enter 1st number:";
         cin>>firstNumber;
         cout<<"enter 2nd number:";
         cin>>secondNumber;
    
         // get the result of a + b
         result = firstNumber + secondNumber;
         count<< "you get " << result << endl;
    
         // Either.... 
         system( "pause" );
         // Or...   (but not both ..)
         cout << "Press any key to continue" << endl;
         getch();
    
         return ( 0 );
    }
    Good Luck!

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    // get the result of a + b
    count<< "you get: result";

    and if u want it to show the result of the numbers u enterd
    u got to put the result after the to

    "cin>>s"
    or else it will just show some results of some numbers from the memory or whatever

    try this:


    #include <iostream.h>
    #include <stdlib.h>
    int main()
    {
    // declare variables

    int firstNumber;
    int secondNumber;
    int result;

    //read variables content

    cout<<"please enter 1st number:";
    cin>>firstNumber;
    cout<<"enter 2nd number:";
    cin>>secondNumber;
    result= firstNumber + secondNumber;
    // get the result of a + b
    cout<< "you get:"<<result;
    system("pause");
    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code Review for upcoming contest
    By Darryl in forum Contests Board
    Replies: 2
    Last Post: 02-28-2006, 02:39 PM
  2. Problem : Threads WILL NOT DIE!!
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 01:37 PM
  3. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM