| | |
Desperate and eagar to learn!!!
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2006
Posts: 6
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Jul 2005
Posts: 1,755
Reputation:
Solved Threads: 283
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.
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.
•
•
Join Date: May 2006
Posts: 6
Reputation:
Solved Threads: 0
•
•
•
•
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.
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?
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
•
•
Join Date: Jul 2005
Posts: 1,755
Reputation:
Solved Threads: 283
C++ Syntax (Toggle Plain Text)
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:
C++ Syntax (Toggle Plain Text)
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:
C++ Syntax (Toggle Plain Text)
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.
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
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
•
•
Join Date: Jul 2005
Posts: 1,755
Reputation:
Solved Threads: 283
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.
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.
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
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
•
•
Join Date: May 2006
Posts: 6
Reputation:
Solved Threads: 0
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:
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)
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); }
•
•
Join Date: Jul 2005
Posts: 1,755
Reputation:
Solved Threads: 283
C++ Syntax (Toggle Plain Text)
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
![]() |
Other Threads in the C++ Forum
- Previous Thread: Error in class constructor
- Next Thread: Need help in files
| Thread Tools | Search this Thread |
Tag cloud for C++
api array arrays based beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library lines linker list loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference return rpg search simple spoonfeeding string strings struct temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






