944,221 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2069
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
May 3rd, 2006
0

Desperate and eagar to learn!!!

Expand Post »
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!!!!!!!

Letish
Reputation Points: 10
Solved Threads: 0
Newbie Poster
letish is offline Offline
6 posts
since May 2006
May 3rd, 2006
0

Re: Desperate and eagar to learn!!!

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
May 3rd, 2006
0

Re: Desperate and eagar to learn!!!

Quote originally posted by Lerner ...
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.


Quote originally posted by Lerner ...
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

Letish
Reputation Points: 10
Solved Threads: 0
Newbie Poster
letish is offline Offline
6 posts
since May 2006
May 3rd, 2006
0

Re: Desperate and eagar to learn!!!

C++ Syntax (Toggle Plain Text)
  1. gets(CT);
  2. while((int)CT != 13)
  3. {
  4. cout<<"\nYou did not press enter\nPlease press enter to continue\n";
  5. gets(CT);
  6. }

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:

C++ Syntax (Toggle Plain Text)
  1. gets(CT);
  2. while(CT != '\n')
  3. {
  4. cout<<"\nYou did not press enter\nPlease press enter to continue\n";
  5. gets(CT);
  6. }


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:
C++ Syntax (Toggle Plain Text)
  1. for(int i = 0; i < 70; i++)
  2. cout << '\n';
  3. 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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
May 3rd, 2006
0

Re: Desperate and eagar to learn!!!

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.
Reputation Points: 185
Solved Threads: 28
Posting Whiz in Training
dwks is offline Offline
269 posts
since Nov 2005
May 5th, 2006
0

Re: Desperate and eagar to learn!!!

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???
Reputation Points: 10
Solved Threads: 0
Newbie Poster
letish is offline Offline
6 posts
since May 2006
May 5th, 2006
0

Re: Desperate and eagar to learn!!!

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
May 6th, 2006
0

Re: Desperate and eagar to learn!!!

Reputation Points: 185
Solved Threads: 28
Posting Whiz in Training
dwks is offline Offline
269 posts
since Nov 2005
May 16th, 2006
0

More Help Needed

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:




C++ Syntax (Toggle Plain Text)
  1. int coincheck(void)
  2. {
  3. ofstream outfile("c:\data.txt",ios::out); //creates outfile
  4. if(!outfile)
  5. {
  6. cerr<<"error:cant open\n";
  7. exit(1);
  8. }
  9. //cout<<"please enter your coin.\n";
  10. cin>>coin;
  11. cout<<"\n!!! - "<<coin;
  12. do{
  13. for (i=0;(c=getchar()) != '\n';++i)
  14. {
  15. coin[i] = c;
  16. if(isdigit(c))
  17. {
  18. cout<<"gifgvhcfj";
  19. outfile<<c;
  20. digit=1;
  21. }
  22. else
  23. {
  24. cout<<"must be a number/n";
  25. digit=0;
  26. exit(1);
  27. }
  28. }
  29. outfile.close();
  30. }while (coin !=0);
  31. ifstream infile("c\data.txt",ios::in); //opens file
  32. if(!outfile)
  33. {
  34. cerr<<"error:cant open\n";
  35. exit(1);
  36. }
  37. infile>>newcoin;
  38. infile.close();
  39. exit(1);
  40. return (0);
  41. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
letish is offline Offline
6 posts
since May 2006
May 16th, 2006
0

Re: Desperate and eagar to learn!!!

C++ Syntax (Toggle Plain Text)
  1. char input[] = "123"; //store input as a string
  2. int len = strlen(input); //determine length of input
  3. int digit = 1; //assume all char of input are digits
  4. for(int i = 0; i < len; ++i) //look at each char of input
  5. {
  6. if(!isdigit(input[i])) //if a char of input is not a digit
  7. {
  8. digit = 0; //change value of digit to indicate a non-digit char found
  9. i = len; //stop loop
  10. }
  11. }
  12. return digit; //digit will still be one if all char of input are digits
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Error in class constructor
Next Thread in C++ Forum Timeline: Need help in files





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC