Problems using while loop with cin.get()

This is a discussion on Problems using while loop with cin.get() within the C++ Programming forums, part of the General Programming Boards category; Hi friends; I am tring to write a program in which I have to enter different kind of input conataining ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    15

    Problems using while loop with cin.get()

    Hi friends;

    I am tring to write a program in which I have to enter different kind of input conataining integers and characters. Each input is then checked for correct spelling and correct formatting. I am using cin.get() to store the input. Everything is working just fine. It is over 500 lines long program. The problem is that each time I want to enter new input, I have to start program again. While loop does not seem to work here. What I want is that each time I am finished entering one input, I should have the choice to enter n to terminate program. If I use gets() instead of cin.get, then while loop works fine but then input gets stored incorrectly. I hope I have made my problem clear. Can ANYONE here help?

    This is a grossly cut-down version of the program.


    #include <iostream.h>
    #include <conio.h>
    #include <dos.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>
    #include <ctype.h>

    int main()
    {

    clrscr();
    char String[35];
    char ans = 'y';

    while ( ans != 'n') {
    clrscr();
    cout << "Enter text:";
    cin.get(String, 35);
    //gets(String);
    cout << "\nYou entered: " << String;
    cout << "\n\n\nEnter more text. Enter n if you want to terminate.";
    cin >> ans;


    } // end while loop


    return 0;
    }
    arj

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    C'mon, use the code tags already!

  3. #3
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Hello,

    The function cin.get() was designed to extract unformatted data from stream. Though, the function gets() gets a string from stdin.

    These two function have there instances, but none alike. I would recommend using cin.getline(). getline() gets a line from stream. It also extracts characters from the stream and stores them into successive locations in the array pointed by s. Learn more about this function by clicking the highlighted link above.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    15
    Hi Stack Overflow

    Thanks for the tip. I tried using cin.getline(). Unfortunately that did not make any difference either. While loop does work properly with cin.get() or cin.getline(). If I enter n to terminated the program, the program does get terminated, but in case I want to continue adding more text, I am unable to do that. Thaks for the tip anyway.

    Arooj
    arj

  5. #5
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    Search the boards and you will find many answers since this a common problem. Your leaving the '\n' in the buffer you need to ignore any characters after you type y to continue.
    Code:
    cin.ignore(std::numeric_limits<streamsize>::max(),'\n'); will ignore all characters in the stream
    [edit]
    As a side note you should use the standard headers As you will no doubt be told countless times if you post code iostream.h etc isn't standard <iostream> is.
    Last edited by manofsteel972; 11-28-2004 at 01:05 AM.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-01-2008, 09:09 AM
  2. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 06:19 AM
  3. "for" loop problems....
    By hayai32 in forum C Programming
    Replies: 4
    Last Post: 05-04-2005, 01:20 AM
  4. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 07:26 PM
  5. problems with greatest to least sorting
    By Mr_Jack in forum C++ Programming
    Replies: 2
    Last Post: 03-11-2004, 09:12 PM

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