Hello All,

I am hoping someone can help. I am working on a project that requires me to create a telephone VDU system. I have done all the hard stuff but it's just some of the easier bits that are evading me like...

a) The user must press "enter" to continue and not any other key. I have tried to get the ASCII code from a character entered but am failing miserably

b) Time remaing must be displayed in the bottom right hand corner. Apart from /t/t/t/t/n/n/n/n I don't know any other way to do it.


I have to perfect this program in the next 4 days so any help would be greatly appreciated. Also I don't doubt that I will come across more hiccups the closer I get to completion!!!!!!!

:twisted: Letish:twisted:

Frequently the success of the answer depends on the success of the question.

a) Are you using a GUI like Windows or Mac or something else or are you just trying to get the ASCII value of the enter key or are you just trying to get the value of a given char, etc.? In console programming you can get the ASCII value of any char value by casting the char to an int. If you want to close a window to continue the program but only do so if the user pushes the enter key, then that will be OS dependent.

b) What do you mean by remaining time? The remainder of 60 seconds minus how many seconds the window has been open ? The days, hours and minutes until Xmas?

Or do you mean how to position the cursor in a console program so you can locate it in the lower right hand corner of the screen? The tab, newline things is okay for that. Otherwise you could use Console methods for the Windows class if you are using a Windows OS. Other OS should have some method as well.

a) Are you using a GUI like Windows or Mac or something else or are you just trying to get the ASCII value of the enter key or are you just trying to get the value of a given char, etc.? In console programming you can get the ASCII value of any char value by casting the char to an int. If you want to close a window to continue the program but only do so if the user pushes the enter key, then that will be OS dependent.

You got a bit technical for me but we are using Windows computers but Borland C++ compiler. What I'm hoping for is something to the equivalent to

cout<<"Please press enter to continue\n";
gets(CT);
do {
cout<<"\nYou did not press enter\nPlease press enter to continue\n";
gets(CT);

} while(CT!=ASCII13);

I don't know what "casting the char to an int" means but it sounds as if it would help.

b) What do you mean by remaining time? The remainder of 60 seconds minus how many seconds the window has been open ? The days, hours and minutes until Xmas?

In this program the user enters numbers representing the money they are spending on the call. My program then works out how many seconds they are entitled to and counts it down in the bottom right hand corner. The time remaining bit is not a problem but rather how to get it into the bottom right hand corner of the screen. At the moment I an using this code.

for (n=total; n>0; n--)
{
clrscr();
cout<<number<<"\n\a";
cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t\t\t\t\t"<<n<<"\n";
wait (1);
}

There must be a way to define the x,y axis of the screen but I can't find it.


Thanks for the input so far

:twisted: Letish:twisted:

gets(CT);
while((int)CT != 13)
{
  cout<<"\nYou did not press enter\nPlease press enter to continue\n";
  gets(CT);
}

Thats a cast of the char CT to an int in the while conditional. Note the use of a while loop rather than a do/while loop. I don't use C often but those that do appear to prefer other options than gets(), too, but that's beside the point.

Alternatively you can forget the cast and just compare chars like this:

gets(CT);
while(CT != '\n')
{
  cout<<"\nYou did not press enter\nPlease press enter to continue\n";
  gets(CT);
}

where '\n' is the char used by the computer when you hit the enter key.

You could always use a loop to position the cursor:

for(int i = 0; i < 70; i++)
  cout << '\n';
cout << timeRemaining;

I believe it's the GoTo() function in Windows that allows you to reposition the cursor based on an x/y coordinate, but I don't use it.

It's gotoxy for Borland C++, I believe.

And you should use fgets() instead of gets().

You'll also definitely need to use getch() and kbhit(). Google for them for more info.

Thank you both so much, the gotoxy and the casting of char to an int worked a miracle. I have another problem. I am trying to validate an input so that only and a number can be entered... any ideas???

You can either do it yourself or utilize the behavior of streams. Either way it's not pretty. You could:

Method A:
store user entry as a string.
Check each char of string to see if it is a digit
If only digits present, covert string entered to numerical format using ostringstream or atox() where x could be i for int or f for float/double or whatever.

Method B:
see if stream is valid after user input
If it isn't then invalid input and clear the input buffer
Else, see if anything left in input buffer.
If there is then input was invalid.

If number can be something other than an int (float, long, ratio, etc) then it's even messier.

Thank you for your help so far but now I need some serious guidance.

I am attempting to check whether a number entered is a digit. I have devised that the way to do this is to create a character array and save the user input there. Then I open/create a text file. USing a for loop I check that they are all digits. If they are my main program should continue on the return of the value 1 in "digit".

It took me ages to get the open/write/read file to work and now my program does the loop but makes me input too many numbers and wont move on. The user must be able to input a figure of up to 3 digits... If someone could help I would be extremely grateful... I may even send flowers....;) :lol: ;)

int coincheck(void)
{
ofstream outfile("c:\data.txt",ios::out); //creates outfile
if(!outfile)
{
cerr<<"error:cant open\n";
exit(1);
}
//cout<<"please enter your coin.\n";
cin>>coin;
cout<<"\n!!! - "<<coin;
do{
for (i=0;(c=getchar()) != '\n';++i)
{
coin[i] = c;
if(isdigit(c))
{
cout<<"gifgvhcfj";
outfile<<c;
digit=1;
}
else
{
cout<<"must be a number/n";
digit=0;
exit(1);
}
}
outfile.close();
}while (coin !=0);
ifstream infile("c\data.txt",ios::in); //opens file
if(!outfile)
{
cerr<<"error:cant open\n";
exit(1);
}
infile>>newcoin;
infile.close();
exit(1);
return (0);
}
char input[] = "123";  //store input as a string
int len = strlen(input); //determine length of input
int digit = 1;      //assume all char of input are digits
for(int i = 0; i < len; ++i)   //look at each char of input
{
   if(!isdigit(input[i]))  //if a char of input is not a digit
   {
     digit = 0;  //change value of digit to indicate a non-digit char found
     i = len;    //stop loop
    }
}
return  digit;  //digit will still be one if all char of input are digits

Lerner = My angel in disguse!!!!

So it seems I was making things too hard for myself. I have tried your cod but although it goes into the for loop it doesn't seems to go into the isdigit loop. I put some screen outputs in to test it and they are not printing. Could you point me in the right direction for a solution?

int coincheck(void)
{
      cout<<"\n coincheck.\n";
      cin>>coin;
      cout<<"\n!!! - "<<coin;
      do{
       for(int i = 0; i < len; ++i)   //look at each char of input
    {
               cout<<"for loop";
       if(!isdigit(input[i]))  //if a char of input is not a digit
        {
                     cout<<"\nIsdigit loop";
           digit = 0;  //change value of digit to indicate a non-digit char found
                     cout<<"\ndigit = "<<digit;
           i = len;    //stop loop
         }
    }
   return  digit;  //digit will still be one if all char of input are digits
       }while (coin !=0);
}

What I should have also told you is that this is a function in a much bigger program that I am writing. The user has to enter on of 5 coin choices 5, 10, 20, 50, 100. To terminate this loop they are to simply enter 0. I am okay checking the values in my main with an if statement using a lot of OR's but decided to write a function to check if it was a digit to stop people inputting letters. Another problem I have is with my for loop... it goes on for too long if they just want to enter the number 5

int coincheck(void)
{
      cout<<"\n coincheck.\n";
      cin>>coin;
      cout<<"\n!!! - "<< coin;
      
       for(int i = 0; i < len; ++i)   //look at each char of input
      {
         cout<<"for loop";
         if(!isdigit(input[i]))  //if a char of input is not a digit
        {
           cout<<"\nIsdigit loop";
           digit = 0;  //change value of digit to indicate a non-digit char found
           cout<<"\ndigit = "<<digit;
           i = len;    //stop loop
         }
    }
   return  digit;  //digit will still be one if all char of input are digits   
}

I cleaned up the code a bit to demonstrate the problem(s) clearer. First, the do/while loop doesn't have anything to do with validating the input, so I removed it for now. Second, I don't see where you declared len, or assigned it a value. Third, you need to adjust variable names to suit your code and not do a crude cut and paste. Once you've cleaned that up you should have a valid protocol to validate a single user input stored in coin.

Now, if you want to force user to keep inputting information until valid input for coin has been done you could wrap the above basic validating protocol in a loop.

int coincheck(void)
{
      cout<<"\n coincheck.\n";

      do                                                           //at least one time
      {
        cout << "input a value for coin" << endl;   //ask for user input
        cin>>coin;                                              //get user input
        cout<<"\n!!! - "<< coin;                           //display user input
                                                                     //declare and assign value to len here
        for(int i = 0; i < len; ++i)   
        {
           cout << "for loop";
           if(!isdigit(input[i]))  
           {
             cout<<"\nIsdigit loop";
             digit = 0;  
             cout<<"\ndigit = "<<digit;
             break;                                   //break out of loop now 
           }
        }

        //if user has entered bad input before digit will be 0 even if 
        //current input is valid, so need a mechanism to change 
        //value of digit back to 1 if appropriate.
        //To do that look at value of i after the for loop has ended.

        if(i == len) //loop evaluated all char in input and all were valid
           digit = 1;  //gaurantee digit indicates success

      }while(digit = 0);  //if digit is 0 then invalid input occurred so do it all again

   return  digit;  //digit will always be one under this scenario so why bother
}

Fourth, this function just checks (or ensures, depending on which version you use) that the input into coin is valid. What you do with the input into coin if it's valid is another problem. Perhaps you could wrap this function in another loop or another function and set it up to stop input into coin when coin is 0.

coin = 1;  //set coin to something other than the termination value
do           //do coincheck() at least one time
{
   coincheck();  //get a valid coin

   if(coin != 0)
      //store coin in a container to use later when all coins have been entered and get next coin
}while(coin != 0);  //stop if coin equals 0
This article has been dead for over six months. Start a new discussion instead.