3 questions

This is a discussion on 3 questions within the C++ Programming forums, part of the General Programming Boards category; I'm writting (have pretty much finished it now) a console program that takes two sentences works out the difference between ...

  1. #1
    The Earth is not flat. Clyde's Avatar
    Join Date
    Mar 2002
    Posts
    1,420

    3 questions

    I'm writting (have pretty much finished it now) a console program that takes two sentences works out the difference between them and applies those changes to each line of a file. What i would like to know is:

    1) When i run my program through my IDE (codewarrior) once the program has finished the dos console stays open untill a key is pressed, if however the program is run straight from its .exe icon the console instantly closes as soon as the program has finished. Since i want the user to be able to read error messages i was wondering if anyone could tell me a line of code that will make the program wait for a key to be pressed before closing the console even when run from it's .exe icon.

    2) I have the user enter a string of numbers followed by a character those numbers are stored in a vector<int>, the code i'm using is:

    Code:
    while (cin >> i)
         spaces.push_back(i);
    where i is an int and spaces is a vector<int>. This works fine but seems clumsy, is there a simple way for me to have the user enter numbers (all on one line separated by spaces) into a vector<int> without asking him to finish off with a character?

    3) I ask the user a series of yes and no questions where they reply with either 'Y' or another character, currently i'm just using
    Code:
    cin >> c
    where c is a character, thing is if they type in "YY" to the first question by mistake the second question catches the second Y. Is there a way to avoid this other than using fgets()? IE. Is there some way i can flush the input stream after the first question is answered so i lose the second character?

    Thanks in advance for any help provided

  2. #2
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    1/ getch();

    3/cout << flush;

    or

    cin.clear();

  3. #3
    The Earth is not flat. Clyde's Avatar
    Join Date
    Mar 2002
    Posts
    1,420
    I realise i can just use getch() (and i probably will) but i was wondering what the IDE does, since when I run it through the IDE the text telling the user to press a key to terminate the app appears in the border of the dos console.

    cin.clear() does not work, it merely removes any error status from the input stream. I will try cout << flush but i presummed that would flush the output buffer, and i didn't think that my extra characters were in that buffer.... but you may be right, i'll try it out.

    Thanks for the advice.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    1) cin.get();
    2) In this case a sentinel value is just fine.
    3) cin.ignore();

    -Prelude
    My best code is written with the delete key.

  5. #5
    The Earth is not flat. Clyde's Avatar
    Join Date
    Mar 2002
    Posts
    1,420
    Thanks prelude cin.ignore() was exactly what i wanted , and whilst i'm still puzzled at what my IDE is doing, cin.get() works fine

  6. #6
    Nor
    Nor is offline
    h ֆhr s Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    Ok. where do you read about all these functions?
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  7. #7
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    Google. Search the forums. Think

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 08:44 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM

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