Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,110 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,727 people online right now. Registration is fast and FREE... Join Now!




can any1 tell me what's wrong with this?

 
Reply to this topicStart new topic

can any1 tell me what's wrong with this?

siren128
17 Apr, 2007 - 05:34 AM
Post #1

New D.I.C Head
*

Joined: 13 Apr, 2007
Posts: 18


My Contributions
hi guys... I seriously need help...
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..

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...

and all I could come up with is this
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!!!!
User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: Can Any1 Tell Me What's Wrong With This?
17 Apr, 2007 - 05:54 AM
Post #2

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,025



Thanked: 35 times
Dream Kudos: 125
My Contributions
well, that worked fine on windows compiler. You might be facing some problem due to buffering. Just flush your standard input buffer when you start every iteration or whenever you take input [whereever you would like to]. It is not a great idea to solve a problem but I guess it might solve the problem.

all the best. smile.gif

This post has been edited by AmitTheInfinity: 17 Apr, 2007 - 05:55 AM
User is online!Profile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 09:38PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month