c++ not showing a variable

This is a discussion on c++ not showing a variable within the C++ Programming forums, part of the General Programming Boards category; Hi... Im a noob... I just copied that into my file thing, and I have no idea what it does... ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    43

    c++ not showing a variable

    Hi... Im a noob...
    I just copied that into my file thing, and I have no idea what it does...
    but thats not the problem

    2 things
    1: I need a good tutorial to learn C++ off
    2: the variable "result" isnt showing and I dont know how to edit the code below to make it show
    (I added the stuff involving the variables, a,b, result)
    please help me...
    Code:
    #include <ios>      // Required for streamsize
    #include <iostream>
    #include <istream>
    #include <limits>   // Required for numeric_limits
    using namespace std;
    
    void myflush ( std::istream& in )
    {
      in.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
      in.clear();
    }
    
    void mypause() 
    { 
         
         
         
      std::cin.get();
    } 
    
    int main()
    {
      int number;
      
      int a, b;
      int result;
      
      a=5;
      b=2;
      a=a+1;
      result=a-b;
      std::cout << result; (<<< this line causes it not to pause so I can see what I did)
      return 0;
      // Test with an empty stream
      std::cout<<"Hello, world!\n" ;
      mypause();
    
      // Leave extra input in the stream
    
      myflush ( std::cin );
      mypause();
    
      std::cin.get();
    }

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Try this:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	
    	int a = 5;
    	int b = 3;
    
    	int result = a + b;
    	cout<<result<<endl;
    
    	
    	cout<<"Hit return to exit."<<endl;
    	cin.get();  //to keep the console window open for your viewing pleasure
    
    	return 0;
    }

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    43

    Wow

    WOW!

    Thanks thats simpler
    Thank you very much!

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    There's no real reason to change the pausing part of that code, IMO. Your original code is more adaptable and will work as your program gets more complicated. For example, 7stud's version will no longer work if you start reading from cin operator>>. The changes to the variable declarations and initialization are good practice, though.

    The real problem is the return 0. That causes the program to end, which means that all the code after it will not be run (including the pauses). Just remove the return 0 and put it at the very end of the main function.

    Code:
    #include <ios>      // Required for streamsize
    #include <iostream>
    #include <istream>
    #include <ostream>  // Required for endl
    #include <limits>   // Required for numeric_limits
    using namespace std;
    
    void myflush ( std::istream& in )
    {
      in.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
      in.clear();
    }
    
    void mypause() 
    { 
      std::cin.get();
    }
    
    int main()
    {
      int a = 5;
      int b = 2;
    
      a = a + 1;
      int result = a - b;
      cout << result << endl;
    
      cout << "Hit return to exit." << endl;
    
    //  myflush ( std::cin ); <-- Not needed, no input was used in this program.
      mypause();
    
      return 0;
    }
    Last edited by Daved; 01-16-2007 at 10:06 PM.

  5. #5
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    The tutorials on this site are good try hitting those up, if you are serious about learning C++ hit up the book listing at the top of this forum.

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    43

    hm

    Well I will have to change it a bit later...

    Im progressing fast I think

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about printing a variable???
    By Hoser83 in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2006, 12:57 PM
  2. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 11:07 AM
  3. Replies: 2
    Last Post: 04-12-2004, 01:37 AM
  4. write Variable and open Variable and get Information
    By cyberbjorn in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 01:30 AM
  5. Variable question I can't find answer to
    By joelmon in forum C++ Programming
    Replies: 3
    Last Post: 02-12-2002, 03:11 AM

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