Loop Trouble

This is a discussion on Loop Trouble within the C++ Programming forums, part of the General Programming Boards category; ***Edit** New error: Code: #include <iostream> #include <cstring> #include <windows.h> #include <stdlib.h> #include <time.h> #include <mmsystem.h> using namespace std; int ...

  1. #1
    Games!? pujuman's Avatar
    Join Date
    Dec 2004
    Location
    Idaho, U.S.A.
    Posts
    14

    Loop Trouble ***New Error***

    ***Edit** New error:


    Code:
    #include <iostream>
    #include <cstring>
    #include <windows.h>
    #include <stdlib.h>
    #include <time.h>
    #include <mmsystem.h>
    using namespace std;
    
    int ans1int=0;
    int ans2int=0;
    int ans3int=1;
    char ans1[4];
    char ans2[4];
    char f1[2];
    char f2[2];
    char m1[2];
    char m2[2];
    char f1f2[10];
    char m1m2[10];
    char f1m1[10];
    char f1m2[10];
    char f2m2[10];
    char f2m1[10];
    
    int main()
    {
        while(ans3int==1){
            while(ans1int==0){
                cout<<"Fathers code - letter 1"<<endl;
                cin.getline(f1, 2);
                cout<<"Fathers code - letter 2"<<endl;
                cin.getline(f2, 2);
                strcat (f1f2, f1);
                strcat (f1f2, f2);
                cout<<"Your father is "<<f1f2<<" correct? [Yes/No]"<<endl;
                cin.getline(ans1, 4);
                if (strcmp(ans1, "Yes")==0 || strcmp(ans1, "yes")==0)
                    ans1int=1;
            }
    When I say no instead of yes, it gives me HhhH (or whatever I input), example: I say Hh the first time, then say no. So I go through again saying hH, and it should say hH back to me, but instead it says HhhH.

    How do I clear a char?
    Last edited by pujuman; 02-22-2005 at 06:30 PM.

  2. #2
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Sounds like a case of not clearing the keyboard's buffer (You have leftover input from the keyboard that's still wanting to be read).

    This FAQ article is mighty handy on the topic:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rewriting a for loop as a while/do-while loop
    By Ashfury in forum C++ Programming
    Replies: 7
    Last Post: 04-27-2007, 03:20 PM
  2. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 03:45 AM
  3. loop needed also how to make input use letters
    By LoRdHSV1991 in forum C Programming
    Replies: 3
    Last Post: 01-13-2006, 05:39 AM
  4. trouble with a for loop with an if statement nested in
    By phoenix-47 in forum C++ Programming
    Replies: 4
    Last Post: 12-14-2005, 04:24 PM
  5. I need help as soon as possible.
    By hyrule in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 05:49 PM

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