small code question

This is a discussion on small code question within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream.h> int main () { int x; x = 5; int y; cout << "enter choice here: "; ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    7

    small code question

    Code:
    #include <iostream.h>
    int main ()
    {
      int x;
      x = 5;
      int y; 
     
     cout << "enter choice here:  ";
      cin >> y;
      if (y == x)
       cout << "yes";
       else
      cout << "no";
      return 0;
    }
    well, a small question. when I run this (using bloodshed.net Dev-c++ compiler), after pressing enter after entering any number, letter etc, the window closes.

    yes this is noobish, and yes ive looked around.

    i THINK my if/else and cin tags are all right, but can someone just tell me how to work that (you should be able to tell what im trying to do. guess a number, if x, output "yes" if not output "no".

    thanks.

    this would solve this problem and others for me.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    16
    at the end, put cin.get(); , before the bracket and return 0 thing that is.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    7
    that's one of the things, well, the only thing i've tried. doesn't help. or at least not for me....

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    start your program from the command prompt.

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    7
    you mean from run>>command ?
    from command prompt?

    how exactly would i do that? ill try, sorry.....

    edit: oh, i got it to work..and it worked.

    but....why....and is there a way to not go to cmd and type in the path etc? thanks.
    Last edited by nigy; 04-03-2005 at 12:01 AM.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    16
    Hrmm.. that works for me... strange.

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    7
    oh, i just edited my last post saying i got it to work via cmd.

    buy why does it work there...

    skynet-systems....so you just inserted the code into a compiler, compiled, ran it and it worked? did you enter a number and get a response?

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Quote Originally Posted by nigy
    oh, i just edited my last post saying i got it to work via cmd.

    buy why does it work there...
    Because you allready have a console provided by cmd.exe. When you double click the program and it exits it's console is closed.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    Apr 2005
    Posts
    7
    Ok, yes, all goes well in command prompt, but when running it, will those codes let me see my final product? using
    Code:
    cin.get();
    , the window stays open for a simple
    Code:
    cout << "__";
    , but anything requiring user input, aka
    Code:
     cin >>___;
    , after entering something and pressing enter, the window finally closes, or a program that counts down starting with some number manually inputed, when it finally reaches 0, the window closes.

    yes, im takign tooo long to say this....but the one on one of salem's links shows mea "C++ implementation using C++" looks a lot like just
    Code:
    cin.get();
    .

    ahhhhh, sorry, im rambling. will any of that code in the link help hte window open...for ever untill closed?

  11. #11
    *this
    Join Date
    Mar 2005
    Posts
    498
    ok so...
    Code:
    cin.get();
    is a function that gets a variable from the keyboard, so basically by putting it at the end of your file, just delays it untill it gets that input from the keyboard.
    basically what people are saying is that you should have a input at the end before it closes so you can see whats going on.

    if you want your program to keep doing it untill you want it to exit by entering a special variable (like exit, 0, -1...etc)

    than your going to want to put it in a while loop.
    for example.
    Code:
    #include <iostream>     //iostream.h is an old header file, better to use iostream
    using namespace std;
    
    int main ()
    {
       int x;
       x = 5;
       int y; 
     
       while( x != -1 )
       {
          cout << "enter choice here (-1 to exit) :  ";
          cin >> y;
          if (y == x)
             cout << "yes";
          else
             cout << "no";
       }
       return 0;
    }

  12. #12
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    Yes, but you would also want to add a condition in the while loop to set x to -1 in order to exit.
    To code is divine

  13. #13
    Registered User
    Join Date
    Apr 2005
    Posts
    7
    Code:
    #include <iostream>
      
      
    using namespace std;
    
      int main (void)
    {
         
      int x;
      x = 5;
      int y; 
      int z;
      
      
      
      while (x != y)
      {
      cout << "enter a number here: ";
      cin >> y;
      
      
      if (y == x)
      
      cout << "yes, oh boy, you got it.";
      
      else if (y > x)
      cout << "no, ur wrong. It's lower. type 0 and press enter to try again.";
      
      else
      cout << "no, ur wrong. It's higher, type 0 and press enter to try again.";
      cin >> y;
    }
    
    
      return 0;
    }
    this is what i have, similar to what you said josh, and smurfs is right. I think.

    My next step is to ad a random function to X so it's not always 5, but looking around ive not found a simple way to have it be between 1 and 100. YOu can give me the code if you really feel like it or not...your choice.

    is there a way to do do
    Code:
    if (y == -10
    exit
    is there a function in c++ that closes the window after completing requirement of sorts?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Tutorial sample code question
    By Kalleos in forum C Programming
    Replies: 2
    Last Post: 01-16-2009, 12:20 PM
  2. A small question about fork( )
    By mlhazan in forum C Programming
    Replies: 4
    Last Post: 08-13-2008, 09:18 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. Hi all, I have a small question...
    By Pandora in forum Windows Programming
    Replies: 3
    Last Post: 03-16-2003, 06:21 AM
  5. question regarding code found on this board
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-23-2002, 09:03 PM

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