|
||||||
| C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
Hello everyone! I just started to learn how to program using C, and was given an assignment I cannot solve. I really need help
I have to create a program that 1) changes an ASCII code entered by the user into the appropriate letter. 2)changes a character entered in by the user into ASCII code 3) Display an ASCII TABLE 4) and when the letter "q" is entered, the program ends... I'm sorry it may be a little confusing, but this is what I was able to come up with so far. Code:
#include <stdio.h>
int main(){
int i;
char c,type;
printf("ASCII character program\n\n");
printf("Input 1, 2, 3, or q\n");
printf("1: ASCII code -> CHAR\n");
printf("2: CHAR -> ASCII code\n"); *This is the menu thats
printf("3: DISPLAY ASCII TABLE\n"); suppose to loop after each
printf("q: End of this Program\n"); choice and the answer*
scanf("%d",&type);
while(type!=113){
if(type=49){
printf(" ASCII code -> CHAR \n");
printf(" input ASCII code\n");
scanf("%d",&i);
printf(" Character for ASCII code %d is -> '%c'\n\n", i,i);}
if(type=50){
printf(" CHAR -> ASCII code \n");
printf(" input a Character\n");
scanf("%c",&c);
printf(" ASCII code for character '%c' is -> '%d'\n\n", c,c);}
if(type=51){
printf("ASCII code table for 1-127\n");
for(i=1; i<=127; i++)
printf("Character for ASCII code %d is -> '%c'\n",i,i);}
}
}
Last edited by Jordan; 04-13-2007 at 07:49 PM. Reason: Added code tags |
| Sponsored Links |
|
|
|
|||||
|
Add another while loop at the top
Code:
while (type=..)
__________________
CodeCall Blog | CodeCall Wiki | Shareware Site | Linux Forum | Write a Blog Don't hesitate to ask any questions that you have! Check out our ASCII Calculator! |
|
|||
|
thank you Jordan for the quick reply! I tried out your advice but I'm still having some difficulties...
I want it to work out like this.. ASCII character program Input 1,2,3, or q 1: ASCII code -> CHAR 2: CHAR -> ASCII code 3: Display ASCII TABLE q: End of this program 1 input ASCII code 123 Character for ASCII code 123 is -> '{' Input 1,2,3, or q 1: ASCII code -> CHAR 2: CHAR -> ASCII code 3: Display ASCII TABLE q: End of this program 2 input a character r ASCII code for character 'r' is -> 114 Input 1,2,3, or q 1: ASCII code -> CHAR 2: CHAR -> ASCII code 3: Display ASCII TABLE q: End of this program q END This is just an example of how I want the program to work, but I can't seem to get the looping part right...please HELP!!!! ![]() |
|
|||
|
thanks again for all the help void!! I tried out your code and it worked out very well...except there seems to be some problems... First of all, I really couldn't use codes such as fflush and so on. I've only learned the real basics such as looping via if, and so on so I really wasn't allowed to use all those advanced codes...sry I didn't mention that before hand...So I used void's program as a reference and fixed up my program, but it just isn't working... this is what I did...
#include <stdio.h> int main() { int i,type; char c; type = 0; printf("ASCII character program\n\n"); while(type!='q') { printf(" Input 1, 2, 3, or q\n"); printf(" 1: ASCII code -> CHAR\n"); printf(" 2: CHAR -> ASCII code\n"); printf(" 3: DISPLAY ASCII TABLE\n"); printf(" q: End of this Program\n"); printf(">>"); scanf("%c", &type); if(type=='1') { printf("ASCII code -> CHAR\n"); printf(" Input ASCII code: "); scanf("%d", &c); printf(" Character for ASCII code %d is -> '%c'\n\n", c, c); } else if(type=='2') { printf("CHAR -> ASCII code\n"); printf(" Input a character: "); scanf("%c", &c); printf(" ASCII code for character '%c' is -> '%d'\n\n", c, c); } else if(type=='3') { printf("ASCII code table for 1-127\n"); for(i = 1; i <= 127; i++) printf("Character for ASCII code %d is -> '%c'\n", i, i); } }} The result of this code was so... $ ./a.exe ASCII character program Input 1, 2, 3, or q 1: ASCII code -> CHAR 2: CHAR -> ASCII code 3: DISPLAY ASCII TABLE q: End of this Program >>1 ASCII code -> CHAR Input ASCII code: 123 Character for ASCII code 123 is -> '{' Input 1, 2, 3, or q 1: ASCII code -> CHAR 2: CHAR -> ASCII code 3: DISPLAY ASCII TABLE q: End of this Program >> Input 1, 2, 3, or q 1: ASCII code -> CHAR 2: CHAR -> ASCII code 3: DISPLAY ASCII TABLE q: End of this Program >>2 CHAR -> ASCII code Input a character: ASCII code for character ' ' is -> '10' Input 1, 2, 3, or q 1: ASCII code -> CHAR 2: CHAR -> ASCII code 3: DISPLAY ASCII TABLE q: End of this Program >>q command 1, 3, and q work perfectly....however 2: CHAR -> ASCII code just isn't working... it won't let me enter the character to convert...I can't figure it out....another problem was that it shows the menu twice after each attempt...please help! |
| Sponsored Links |
|
|
|
|||||
|
First of all, try learn to use the [ code ]-tags.
The problems you have is because you don't use the fflush()-function. When something is entered through the input-stream (stdin) during execution of the application, it will mostly stay in there. When it then tries to get some new stuff through the stream, it will sometimes be... yeah, strange. When using fflush() and stdin together, the stdin will be cleared and you can retrieve new input, without worry. I've made minor changes to your application, actually I've only inserted the fflush()-function, two times. I've commented the code those placed, so you get what's going on. Code:
#include <stdio.h>
int main()
{
int i, type;
char c;
type = 0;
printf("ASCII character program\n\n");
while(type != 'q')
{
printf(" Input 1, 2, 3, or q\n");
printf(" 1: ASCII code -> CHAR\n");
printf(" 2: CHAR -> ASCII code\n");
printf(" 3: DISPLAY ASCII TABLE\n");
printf(" q: End of this Program\n");
printf(">>");
scanf("%c", &type);
/*\
|*| First we get some input from the user, into
|*| _type_, through the stdin (Standard Input Stream).
|*| Then, before we're retrieving new data
|*| (in the if-statement) we have to clear it.
|*| This is done with fflush().
\*/
fflush(stdin);
if(type == '1')
{
printf("ASCII code -> CHAR\n");
printf(" Input ASCII code: ");
scanf("%d", &c);
printf(" Character for ASCII code %d is -> '%c'\n\n", c, c);
}
else if(type == '2')
{
printf("CHAR -> ASCII code\n");
printf(" Input a character: ");
scanf("%c", &c);
printf(" ASCII code for character '%c' is -> '%d'\n\n", c, c);
}
else if(type == '3')
{
printf("ASCII code table for 1-127\n");
for(i = 1; i <= 127; i++)
printf("Character for ASCII code %d is -> '%c'\n", i, i);
}
/*\
|*| Now we got something into _c_, and before
|*| the loop starts over again, we need to clear
|*| it before we again will retrieve something in _type_.
\*/
fflush(stdin);
}
return 0;
}
|
|
|||
|
Void, I actually tried out ur code, exactly as written here, and well, it just isnt working... its still having the same problem...
I've been working on this program for the past few days, and well....it just isn't working out at all...It's starting to get really annoying.. Code:
#include <stdio.h>
int main()
{
int i, type;
char c;
type = 0;
printf("ASCII character program\n\n");
while(type != 'q')
{
printf(" Input 1, 2, 3, or q\n");
printf(" 1: ASCII code -> CHAR\n");
printf(" 2: CHAR -> ASCII code\n");
printf(" 3: DISPLAY ASCII TABLE\n");
printf(" q: End of this Program\n");
printf(">>");
scanf("%c", &type);
fflush(stdin);
if(type == '1')
{
printf("ASCII code -> CHAR\n");
printf(" Input ASCII code: ");
scanf("%d", &c);
printf(" Character for ASCII code %d is -> '%c'\n\n", c, c);
}
else if(type == '2')
{
printf("CHAR -> ASCII code\n");
printf(" Input a character: ");
scanf("%c", &c);
printf(" ASCII code for character '%c' is -> '%d'\n\n", c, c);
}
else if(type == '3')
{
printf("ASCII code table for 1-127\n");
for(i = 1; i <= 127; i++)
printf("Character for ASCII code %d is -> '%c'\n", i, i);
}
}
return 0;
}
AND I want the program to run like so... ASCII character program Input 1,2,3, or q 1: ASCII code -> CHAR 2: CHAR -> ASCII code 3: Display ASCII TABLE q: End of this program 1 input ASCII code 123 Character for ASCII code 123 is -> '{' Input 1,2,3, or q 1: ASCII code -> CHAR 2: CHAR -> ASCII code 3: Display ASCII TABLE q: End of this program 2 input a character r ASCII code for character 'r' is -> 114 Input 1,2,3, or q 1: ASCII code -> CHAR 2: CHAR -> ASCII code 3: Display ASCII TABLE q: End of this program q END However it runs like so... $ ./a.exe ASCII character program Input 1, 2, 3, or q 1: ASCII code -> CHAR 2: CHAR -> ASCII code 3: DISPLAY ASCII TABLE q: End of this Program >>1 ASCII code -> CHAR Input ASCII code: 96 Character for ASCII code 96 is -> '`' Input 1, 2, 3, or q 1: ASCII code -> CHAR 2: CHAR -> ASCII code 3: DISPLAY ASCII TABLE q: End of this Program >> Input 1, 2, 3, or q 1: ASCII code -> CHAR 2: CHAR -> ASCII code 3: DISPLAY ASCII TABLE q: End of this Program >>2 CHAR -> ASCII code Input a character: ASCII code for character ' ' is -> '10' Input 1, 2, 3, or q 1: ASCII code -> CHAR 2: CHAR -> ASCII code 3: DISPLAY ASCII TABLE q: End of this Program >>q What it is doing wrong is when the user enters 2, to change the CHAR to ASCII code, it won't allow the user to enter anything... The program also displays the main menu twice after each attempt...I can't figure it out....I been working like mad on it for a few days, but it isn't working at all..HELPPP!!!! |
|
|||||
|
You're still missing to flush the standard input stream, before starting the while-loop over again. You have to insert a fflush()-function in the start of the while-loop or at the end of it. If you're looking at the last code I posted in this thread, you can see that there's two fflush()-functions, not only one.
The reason why you have to insert one, is because that after you have selected something in the menu, and again inputs something - f.ex. CHAR => ASCII, then the stdin get new data again - and that data, we've to clear. That's by using the fflush()-function at stdin. If you read my comments in the code, you could see it too. Code:
#include <stdio.h>
int main()
{
int i, type;
char c;
type = 0;
printf("ASCII character program\n\n");
while(type != 'q')
{
/* Here's all the other stuff ... */
/* At the end, we need to have this */
fflush(stdin);
}
return 0;
}
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How do I Program another Program? ! | bosco | General Programming | 1 | 06-15-2007 11:15 AM |
| ! Need urgent help ! Drawing program using cygwin | siren | C and C++ | 0 | 05-26-2007 10:51 PM |
| Need help w/ word count program (ASAP) | siren | C and C++ | 1 | 04-23-2007 08:14 AM |
| How to modify a program written in .NET 2.0? | jackyjack | C# Programming | 7 | 03-27-2007 12:26 PM |