Custom Search
Join the PC homebuilding revolution! Read the all-new, FREE 200-page online guide: How to Build Your Own PC!
NOTE: Using robot software to mass-download the site degrades the server and is prohibited. See here for more.
Find The PC Guide helpful? Please consider a donation to The PC Guide Tip Jar. Visa/MC/Paypal accepted.
Results 1 to 3 of 3

Thread: Super basic C++ question

  1. #1

    Super basic C++ question

    #include <iostream.h>

    void main(){
    int x =999;
    cin>>x;
    cout<<x;
    }

    in this book, I'm reading, it says if i eneter a non numeric number like a, it will not read the input and print out what it has, in this case, it is 999. But on my system, it prints out 0. Can someone explain?
    Thanks

  2. #2
    Join Date
    Oct 2001
    Location
    N of the S of Ireland
    Posts
    20,504
    Not sure because I've never used C as such but you could try declaring the variable first - and then give it a value.

    void main()
    {
    int x;
    x = 999;
    cout << x;
    }

    or if you are inputting as a separate process

    void main()
    {
    int x;
    cout << "Enter an integer such as 999\n";
    cin >> x;
    cout << "You entered " << x << " didnt you!";
    }
    Take nice care of yourselves - Paul - ♪ -
    Help to start using BiNG. Some stuff about Boot CDs & Data Recovery Basics & Back-up using Knoppix.

  3. #3
    Join Date
    May 2001
    Location
    Glen Allen, Va. U.S.A.
    Posts
    1,057
    Weird, the preprocessor directive isn't showing the included library. Well, anyway...

    I may be able to help you, but you must fix your code first.I don't think a modern compiler will allow

    Code:
    void main()
    I'm pretty sure gcc doesn't, and its a bad idea anyway. Main should always return something, usually an int.

    I'm quite sure that...
    Code:
    cout<
    won't compile. The operator << has to be complete. Eliminating all your errors in syntax, I think your code should look like this...

    Code:
    #include <iostream> // or <iostream.h> depending on your compiler
    using namespace std;
    
    int x;
    int main()
    {
        x = 999;
        cout << "enter x: ";
        cin >> x;
        cout << x;
        return 0;
    }
    Now then, this exact code typed into vim and compiled with gcc gives an output of 999, which is exactly as I assigned to x in line 7. I input an "a" but it was not assigned to x, I guess because x is an int, not a char.
    Last edited by yawningdog; 11-28-2003 at 11:20 PM.
    “The highest glory of the American Revolution was this: It connected in one indissoluble bond, the principles of civil government with those of Christianity." -John Quincy Adams

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •