Desperate and eagar to learn!!!

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2006
Posts: 6
Reputation: letish is an unknown quantity at this point 
Solved Threads: 0
letish letish is offline Offline
Newbie Poster

Desperate and eagar to learn!!!

 
0
  #1
May 3rd, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,755
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Desperate and eagar to learn!!!

 
0
  #2
May 3rd, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 6
Reputation: letish is an unknown quantity at this point 
Solved Threads: 0
letish letish is offline Offline
Newbie Poster

Re: Desperate and eagar to learn!!!

 
0
  #3
May 3rd, 2006
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.


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
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,755
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Desperate and eagar to learn!!!

 
0
  #4
May 3rd, 2006
  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:

  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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 251
Reputation: dwks has a spectacular aura about dwks has a spectacular aura about 
Solved Threads: 25
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

Re: Desperate and eagar to learn!!!

 
0
  #5
May 3rd, 2006
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.
dwk

Seek and ye shall find.

"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.

"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison

"The only real mistake is the one from which we learn nothing."
-- John Powell
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 6
Reputation: letish is an unknown quantity at this point 
Solved Threads: 0
letish letish is offline Offline
Newbie Poster

Re: Desperate and eagar to learn!!!

 
0
  #6
May 5th, 2006
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???
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,755
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Desperate and eagar to learn!!!

 
0
  #7
May 5th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 251
Reputation: dwks has a spectacular aura about dwks has a spectacular aura about 
Solved Threads: 25
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

Re: Desperate and eagar to learn!!!

 
0
  #8
May 6th, 2006
dwk

Seek and ye shall find.

"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.

"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison

"The only real mistake is the one from which we learn nothing."
-- John Powell
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 6
Reputation: letish is an unknown quantity at this point 
Solved Threads: 0
letish letish is offline Offline
Newbie Poster

More Help Needed

 
0
  #9
May 16th, 2006
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:




  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. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,755
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Desperate and eagar to learn!!!

 
0
  #10
May 16th, 2006
  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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC