CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Nov 2001
    Location
    CA
    Posts
    10

    simple C++ problem

    I can't figure this out. What am I missing in this simple little program? I can build & compile this and there are no errors or warnings. But as soon as I enter a string and press enter everthing goes away.

    #include <iostream>

    using namespace std;

    int main()
    {

    char s[80];

    cout << "Enter a character string" << endl;
    cin >> s;
    cout << "The string you entered is: " << s << endl;

    return 0;
    }



  2. #2
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: simple C++ problem

    Hi,

    There is nothing wrong with your code. The only problem I see will occur if you write over the boundaries which means typing in more than 80 characters...but besides that the program will work fine...

    Did you debug your code?? If you still have the problem it would be useful to see some debug output like the function its crashes etc.

    Ciao, Andreas

    "Software is like sex, it's better when it's free." - Linus Torvalds

  3. #3
    Join Date
    Nov 2001
    Location
    CA
    Posts
    10

    Re: simple C++ problem

    Thank you for replying. I was able to compile and run my program and it does display the first cout statement. I get to type in a string of characters and once I press enter the screen disappears. I wish there was a way to pause after I type in a string and press enter so that my program will go to the 2nd cout statement. I hope this makes sense.

    Thanks Rimby.


  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: simple C++ problem

    I compiled and ran your program. There is nothing wrong with entering characters until you enter a space.

    If you enter a string with spaces, operator >> will stop at the first space. Use getline() if you want to make sure that the entire string is read in:

    cin.getline(s,79);



    Regards,

    Paul McKenzie


  5. #5
    Join Date
    Nov 2001
    Location
    CA
    Posts
    10

    Re: simple C++ problem

    Thank you so much for replying. I made that change. It compiled w/out errors or warnings but as soon as I typed in a string of characters and then pressed enter, the box or screen went away. Perhaps it is a software issue.
    I really like your suggestion!
    Thanks Rimby


  6. #6
    Join Date
    Sep 1999
    Location
    NJ
    Posts
    1,299

    Re: simple C++ problem

    You are writing this under Windows, correct? If so, there's nothing wrong with your program --- it's doing exactly what you told it to do.

    When you run a "Console-mode" program like this under Windows, a "console window" is created just for it. Once the program ends, the windows is destroyed. So, after you enter your text, the program print the line in the window, and then the programs ends, the window closes. It probably happened so fast that you didn't see it print the line.

    If you open a Dos Session manually, and then run the program by typing it's name at the prompt, the window should stay open.

    Truth,
    James
    http://www.NJTheater.com
    http://www.NovelTheory.com
    I don't do it for the points (OK, maybe I do), but rating a post is a good way for me to know if I helped.

  7. #7
    Join Date
    Nov 2001
    Location
    CA
    Posts
    10

    Re: simple C++ problem

    Thank you so much, I was going insane trying to figure out why? Now I can rest about it. Your tip has been a great help.
    Thanks again,
    Rimby


  8. #8
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: simple C++ problem

    Hi,

    Your second 'cout' will be executed but since you exit your application right after printing the second line via the 'return' statement you will probably not see the output very well. You can add a function like 'getchar()' (in Windows you can use '_getch()', defined in conio.h) which is defined in stdio.h. It will wait for another keystroke. So

    #include <iostream>
    #include <stdio.h> // in Windows use conio.h

    using namespace std;

    int main()
    {
    char s[80];
    cout << "Enter a character string" << endl;
    cin >> s;
    cout << "The string you entered is: " << s << endl;

    getchar(); // in Windows use _getch()

    return 0;
    }



    The way James described works of course as well...

    Ciao, Andreas

    "Software is like sex, it's better when it's free." - Linus Torvalds

  9. #9
    Join Date
    Nov 2001
    Location
    CA
    Posts
    10

    Re: simple C++ problem

    I like both ideas. I tried your idea just now and that window still disappears. I can watch it by stepping into and over the function and it works. I just would really like it, if the window would just stay after I enter my string of characters w/out spaces.

    I will keep trying!

    Thanks you again,
    Rimby


  10. #10
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: simple C++ problem

    Hmmm, well my description was not totally clear. 'getchar()' and '_getch()' don't wait for the next keystroke. It's just not the proper description but I was a little bit in hurry. Both functions are reading one character from the input stream in your case 'stdin' (Standard input). Normally you can use them to wait for one keystroke as well. BUT, if some character is still waiting in the input streambuffer this function won't wait, it will immediately read the next character and in your case close the window.

    As Paul already mentioned, if you type in some kind of string with a space, operator '>>' will stop at the space. So the rest of your string will stay in the input streambuffer and if you call 'getchar()' or '_getch()' it will notice that there are characters waiting inside the buffer and will read the first character from it. If you type in

    "This is a string"

    's' will only contain "This ". If you then call 'getchar()' or '_getch()' they will return 'i' since this is the next character which is still inside the buffer.

    That might be the problem, I don't know if your program is running under Windows but normally '_getch()' shouldn't make bigger problems.

    Ciao, Andreas

    "Software is like sex, it's better when it's free." - Linus Torvalds

  11. #11
    Join Date
    Oct 2001
    Location
    Reading, England
    Posts
    98

    Re: simple C++ problem

    Try pressing <Ctrl> + F5 instead of just F5 when you run the program. This will make Visual Studios ask for a keypress before closing the console window when the program exits.

    Hope this helps,

    Oliver


  12. #12
    Join Date
    Nov 2001
    Location
    CA
    Posts
    10

    Re: simple C++ problem

    Hello,

    Someone suggested to me to use:

    system ("PAUSE");

    prior to the return and it worked perfectly. Someone else suggested using "ignore" but I wasn't sure how to do this.

    Rosee



Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured