Small Problem that could be a big problem

This is a discussion on Small Problem that could be a big problem within the C++ Programming forums, part of the General Programming Boards category; Folks, I've been tasked with writing a program that calls functions from a tutor provided class. It stores a essentially ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    17

    Small Problem that could be a big problem

    Folks, I've been tasked with writing a program that calls functions from a tutor provided class. It stores a essentially the functions to create a spelling test program. I ask it for the next question, get some user input to ask if a word is spelt correctly, send the users answer (which is supposed to be a char 'y' or 'n') and am returned a true or false as to whether the user was wrong.

    At this point in time however I cannot stop sending the \0 charachter because when the user inputs a character, they press enter afterwards. The code is written also using a lib supplied by the tutor, that replaces stdio.h, because the idea is learning how to use classes... ReadChar(); simply reads the last charachter from the screen and returns it. If you guys need any further explanation I'll happily provide it.

    Code:
    #include <vcl.h>
    #include "MT262io.h"
    #include "SpellQuiz.h"
    #pragma hdrstop
    
    //---------------------------------------------------------------------------
    
    #pragma argsused
    
    int main(int argc, char* argv[])
    {
    QuizType MyQuiz; // Declare a QuizType object.
    // Your code goes here.
    
      //var's
      char UsrAttempt;
      int Score;
      bool More;  //make this a bool and then test it?
      bool temp;
    
      //Init
      MyQuiz.Initialise();
      Score = 0;
    
      while(More)
      {
            //Display Prompt and get question
            WriteString("Is the following spelt correctly?\n y\n: ");
            WriteString(MyQuiz.GetNextQn());
    
            //Get User Input
            fflush(stdin);
            UsrAttempt = ReadChar();
    
            //Test Guess and print relevant info (this form of if will be run
                                                    //when condition is true)
            temp = MyQuiz.Guess(UsrAttempt);
    
            if (temp == true)
            {
                    Score = Score + 1;
                    WriteString("Correct!");
                    WriteIntPr("Your Score is now ", Score);
            }
    
            //If user guesses wrong the score is not incrimented
            if (temp == false)
            {
                    WriteString("Wrong!");
                    WriteIntPr("Your Score is now ", Score);
            }
    
            else
            {
                    WriteString("Error, neither true or false returned");
            }
    
            More = MyQuiz.AnyMore() == true;
    
      }
    
    
    
    
      getchar();
      return 0;
    }
    I'm aware its quite cumbersome, and for some reason the compiler I'm using (Builder 5) won't allow me to debug most of the lines of the code hence the temp variable. I'm perplexed as to how I can get the users answer and not the \0 char though

    Thanks!!!!
    Homepage: www.sytaylor.net

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Do you know the implementation of ReadChar()? If ReadChar() reads user input into a string and returns the last char of the string then UsrAttempt will always be a null character. However, if ReadChar() is implemented as the name sounds and reads the input into a char variable without turning the single char into a string then your code looks okay to me. You can try outputting the value of UsrAttempt to see what it is to comfirm it is the null char before passing it to the test function.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    17
    It appears you are correct its getting a null charachter. I'm not sure how to work with that?? I need the charachter the user is inputting to answer the question and it seems to make sense that readchar would do it
    Homepage: www.sytaylor.net

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    17
    *Utterly shameless bump*

    Sorry guys but im totally lost and don't know where to start with this issue. I'll accept URLS at this point! Its the one key issue stopping the whole thing from working im semi certain of that...
    Homepage: www.sytaylor.net

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Well, first you need to figure out your booleans
    if (temp == true)
    can be if(temp)
    and
    if (temp == false)
    can (should) be replaced with either
    if(!temp)
    or better,
    else

    Your current else is bad, because it's conditional on if(temp == false), so if it's true then your code will print "neither true or false". Besides, it's impossible for a boolean variable to anything except true or false, so the statement is redundant even if it were logically correct.

    Similarly,
    More = MyQuiz.AnyMore() == true;
    should just be More = MyQuiz.AnyMore().

    See if that changes anything.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    17
    I made the modifications suggested and its still simply hitting the else regardless. The reuslt it gets back from the:

    Code:
    MyQuiz.Guess(UsrAttempt)
    line is false because the program is sending a '\0' charachter instead of a Y or N char. Meaning the user can enter Y or N and the result is still 'false'.

    The other problem seems to be that the program is not looping, when i test for MyQuiz.Anymore, I must be getting a false return somehow, even though there is more than one question.

    Thanks for cleaning that part up though hunter2
    Homepage: www.sytaylor.net

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,657
    the problem with this:

    if (temp == true){}
    if (temp == false){}
    else{}

    is that the 'else' statement executes even when temp is true - remember to use 'else-if' in situations like that.

    if (temp == true){}
    else if (temp == false){}
    else{}

    > bool More;

    that uninitialized bool is just asking for trouble - although using a do/while loop you could avoid the initialization altogether.

    do{}
    while(MyQuiz.AnyMore());

    > The other problem seems to be that the program is not looping, when i test for MyQuiz.Anymore, I must be getting a false return somehow, even though there is more than one question.

    so post the code to the function in question.



    ITSA
    Socket Library!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Small Problem in C, Please Help.
    By DoctorFu in forum C Programming
    Replies: 6
    Last Post: 10-31-2005, 02:06 PM
  2. Small program big problem :-(
    By Fibre Optic in forum C++ Programming
    Replies: 4
    Last Post: 09-20-2005, 08:53 AM
  3. Big HDD problem
    By biosninja in forum Tech Board
    Replies: 4
    Last Post: 08-18-2005, 06:08 AM
  4. a big problem
    By sweet2awy in forum C++ Programming
    Replies: 3
    Last Post: 07-07-2003, 10:27 AM
  5. Need help with something small - formatting problem
    By wireless in forum C++ Programming
    Replies: 0
    Last Post: 05-28-2002, 05:52 PM

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