#include<stdio.h>
    #include<conio.h>
     
    main()
    {
    char c;
     
    printf("Enter the code for the chart: \n");
    do
    {
    scanf("%c",&c);
    switch (c)
    {
    case 'A':printf("Area Chart\n");break;
    case 'B':printf("Bar Chart\n");break;
    case 'U':printf("Bubble Chart\n");break;
    case 'C':printf("Column Chart\n");break;
    case 'N':printf("Cone Chart\n");break;
    case 'Y':printf("Cylinder Chart\n");break;
    case 'D':printf("Doughnut Chart\n");break;
    case 'L':printf("Line Chart\n");break;
    case 'I':printf("Pie Chart\n");break;
    case 'P':printf("Pyramid Chart\n");break;
    case 'R':printf("Radar Chart\n");break;
    case 'S':printf("Scatter Chart\n");break;
    case 'F':printf("Surface Chart\n");break;
    case 'K':printf("Stock Chart\n");break;
    default:printf("The code is not valid.\n");
    }
    }
    while (c=='\n');
    getch();
    }

The program is working fine, but after inputting the first code for the chart, the program terminates. What should i do so that after inputting the first code i can input another?

Recommended Answers

All 7 Replies

Have you considered a loop? That's generally how we repeat things in C. ;)

You can use a loop as Narue suggested and maybe store the codes in an array...as far as i can see, you are not storing any input whatsoever...getch() reads a char, fine, but shouldn't you be storing it as well for, um, maybe you would like to draw the particular chart(s), based on the code later? In that case you would need the input...:)

Re: how can i input more than 1 code?
Have you considered a loop? That's generally how we repeat things in C.

i know that i will use loop. :D here is my previous code.

#include<stdio.h>
#include<conio.h>

main()
{
      char c;
      
      printf("Enter the code for the chart: \n");
      
      do
      {
                    scanf("%c",&c);
                    switch (c)
                    {
                            case 'A':printf("Area Chart\n");break;
                            case 'B':printf("Bar Chart\n");break;
                            case 'U':printf("Bubble Chart\n");break;
                            case 'C':printf("Column Chart\n");break;
                            case 'N':printf("Cone Chart\n");break;
                            case 'Y':printf("Cylinder Chart\n");break;
                            case 'D':printf("Doughnut Chart\n");break;
                            case 'L':printf("Line Chart\n");break;
                            case 'I':printf("Pie Chart\n");break;
                            case 'P':printf("Pyramid Chart\n");break;
                            case 'R':printf("Radar Chart\n");break;
                            case 'S':printf("Scatter Chart\n");break;
                            case 'F':printf("Surface Chart\n");break;
                            case 'K':printf("Stock Chart\n");break;
                            }
                            }
                            while (c='\n');
                            getch();
                            }

it is also working and i can input many code for the chart as i want, but the problem is there is no default, and whenever i put default in the code the output is like this.

Enter the code for the chart:
A
Area Chart
The code is invalid

what should i do now? :D

You should pick an invalid code that terminates the loop, or depend on a failure case of scanf() (such as end-of-file) to break the loop. For example:

#include <stdio.h>

int main(void)
{
    int code;
    
    printf("Enter a code: ");
    fflush(stdout);
    
    while ((code = getchar()) != EOF) {
        if (code == '\n')
            continue;
        
        switch (code) {
            case 'A': printf("Area Chart\n"); break;
            case 'B': printf("Bar Chart\n"); break;
            case 'U': printf("Bubble Chart\n"); break;
            case 'C': printf("Column Chart\n"); break;
            case 'N': printf("Cone Chart\n"); break;
            case 'Y': printf("Cylinder Chart\n"); break;
            case 'D': printf("Doughnut Chart\n"); break;
            case 'L': printf("Line Chart\n"); break;
            case 'I': printf("Pie Chart\n"); break;
            case 'P': printf("Pyramid Chart\n"); break;
            case 'R': printf("Radar Chart\n"); break;
            case 'S': printf("Scatter Chart\n"); break;
            case 'F': printf("Surface Chart\n"); break;
            case 'K': printf("Stock Chart\n"); break;
            default: printf("The code is not valid.\n"); break;
        }
        
        printf("Enter a code: ");
        fflush(stdout);
    }
    
    return 0;
}

On DOS or Windows platforms, Ctrl+Z would end the program. On POSIX platforms, Ctrl+D would end it. This allows you to use all characters as a code if necessary.

When you enter your code,
1) how many keys to you actually press?
2) how many characters, therefore, are in the input buffer?
3) what are they?
4) how many characters does the code = getchar() retrieve?
5) are there any characters left in the buffer? If so, what?

When you enter your code,
1) how many keys to you actually press?
2) how many characters, therefore, are in the input buffer?
3) what are they?
4) how many characters does the code = getchar() retrieve?
5) are there any characters left in the buffer? If so, what?

Since your questions seem to apply more to a mistaken reading of my code, I'll assume the they're are targeted at me. Here are the answers:

1) 2
2) 2
3) The code and '\n'
4) 1
5) Yes, '\n' is left in the buffer.

Now for the question that renders all of yours moot:

6) What does if (code == '\n') continue; accomplish?

;)

In line number : 31 just modify your code as while(ch!='\n');

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.