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.
I need it to keep repeating the menu at the top, until the user enters "q" after the menu. Sorry for the unclearness but any comment will be helpful.. thanks in advance.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 05:49 PM. Reason: Added code tags
Add another while loop at the top
Replace the .. with whatever the ascii value of q equates to.Code:while (type=..)
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!!!!![]()
You're using the assignment-operator, when you're comparing the user-input, with the options to choose. You need to use the compare-operator instead. To optimize the code, I would prefer to use a switch-statement, instead of if's (there by the way, should have been if...elseif...else).
That's how I would do it. As you can see it's using a switch-statement instead of the if-statements. It's usually a good idea to use a switch-statement, if there's many options. But there isn't so many in this one, so we could had used if...elseif...else-statement.Code:#include <stdio.h> /* This is just a little thing, to make our application nicer */ #define SEPARATOR "------------------------------------\n" int main() { int i, c, type; type = 0; /* We need to initialize the type-variable */ /* There isn't a big difference on int's and char's, so we don't */ /* need to use the ASCII-code, we can just you the real character */ while(type != 'q') { 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"); printf(" 3: DISPLAY ASCII TABLE\n"); printf(" q: End of this Program\n"); printf(" >> "); /* I see in your if-statements, you're using the ASCII-codes, */ /* that means, we've to scan the _character_ using %c */ scanf("%c", &type); /* Separator */ printf("%s", SEPARATOR); /* Flush everything in STDIN, so it don't keep it, */ /* until the switch */ fflush(stdin); /* This is our switch-statement */ switch(type) { /* If the user choose '1' */ case '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); break; /* If the user choose '2' */ case '2': printf("CHAR -> ASCII code\n"); printf(" Input CHAR: "); scanf("%c", &c); printf(" ASCII code for character '%c' is -> '%d'\n\n", c, c); break; /* If the user choose '3' */ case '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); break; /* If the user choose non of above */ default: printf("Invalid option, try again...\n"); } /* We need to flush again, before next round */ fflush(stdin); printf("%s", SEPARATOR); } }
By the way, which compiler are you using? I've never seen comments like this;
Like you're using in your code. It isn't standard C, so stop using it.Code:int var = 10; * This is a comment *
Code:C: /* COMMENT */ C++: // COMMENT /* COMMENT */
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!
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; }
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks