Thread: Need a small and big help!

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    1

    Need a small and big help!

    Ok so i just started learning C, and its been cool, but hard.
    Now that i came to the point where im learning to do "while" loops, im having trouble!
    So my code is:
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    void main (void)
    {
        char moikka;
    
        printf ("Hello! Welcome to the Calculator\n");
        printf ("Press x if you want to continue\n\n");
        
                   while(1) {
            scanf("%d", &moikka);
            printf("Your letter was: %d", &moikka);
            if (%d=='x') {
                    break;
            }
            getch();
        }
        printf("\n\nHi");
    
        return;
    
    So what im trying to do here is a calculator menu screen, you run the code, it says hello, then it says that you need to press X if you want to continue (exit the loop). But i dont know how i can do that, i need it to be so that if you press X, it leaves, but if you press the wrong letter, it says "Oops something went wrong" and you can type a letter again, so you will have infinite chances.
    I know i should just watch a youtube video or a guide, but none have helped me.

  2. #2
    Banned
    Join Date
    Aug 2017
    Posts
    861
    it is not your loop it is what you're doing with your other code that is faulty. But you're using conio.h, which is DOS. so that behavior you say you're getting .. humm.
    Code:
    #include <stdio.h>
    #include <conio.h>
    //here main is not declaried properly 
    // void is not valid anymore.
    void main (void)
    {
        char moikka;
    
        printf ("Hello! Welcome to the Calculator\n");
        printf ("Press x if you want to continue\n\n");
         // here you got infinite  loop setup already
                   while(1) {
            //here you're telling look for an int
            //and using char
            scanf("%d", &moikka); 
            //here you telling to print a int
            // and using char
            printf("Your letter was: %d", &moikka);
            // %d makes no sense at all
            // even d == 'x'
            // d is not declared. 
             // you are using the wrong thing
           // moikka is what yo uneeded to use as a char across the board. But you're
          // using int for numbers, so now you're crossing the board using apples and oranges logic. 
            if (%d=='x') {
                    break;
            }
            getch();
        }
        printf("\n\nHi");
    
        return;
    plus your messages says press X if you want to continue and using x to stop continuing.

    take a look at this, I set it up to quit on x because of your break , if you have to put that conio.h back into play then change the getchar() back to getch()
    Code:
    #include <stdio.h>
    //#include <conio.h>
    
    int main (void)
    {
        char moikka;
        
    
        printf ("Hello! Welcome to the Calculator\n");
        printf ("Press x if you want to quit\n\n");
        
       
                   while(1) {
            scanf("%c", &moikka);
            printf("Your letter was: %c\n", moikka);
        
            if (moikka == 'x') {
                    break;
            }
            getchar();
        }
        printf("bye\n");
    
        return 0;
    }
    this one here is something to look at to see how to keep it going until a condition is reached.
    Code:
    #include <stdio.h>
    //#include <conio.h>
    
    int main (void)
    {
        char moikka;
        // you should be using int's for a calculator
        //but for the purpose of loops
           
        while( moikka != 'x') 
        {
            printf ("Hello! Welcome to the Calculator\n");
            printf ("Press x if you want to quit\n\n"
            "Press any other letter to continue\n");
            
            scanf(" %c", &moikka);
            printf("Your letter was: %c\n", moikka);
        }
        printf("bye\n");
    
        return 0;
    }
    Last edited by userxbw; 12-12-2017 at 12:03 PM.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    if (%d=='x')
    You want to use the variable name here:

    Code:
    if (moikka == 'x')
    Note that this only checks for a lower-case 'x'. If you want either case to work, you either need to check for both, or normalize the input to upper- or lower-case (using "isupper()" or "islower()" functions).

    Code:
    char moikka;
    
    // ...
    
    scanf("%d", &moikka);
    Use "%c" to read a character.

    main returns int, not void: FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com

    It's usually considered bad form to do "while(1)" with a break. It's better to put the looping condition directly into the while.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 08-28-2012, 06:11 AM
  2. one small help
    By kumaravel_futu in forum C Programming
    Replies: 20
    Last Post: 04-29-2008, 04:22 AM
  3. Small Help!
    By nkhambal in forum C Programming
    Replies: 7
    Last Post: 11-11-2006, 07:18 AM
  4. A small problem with a small program
    By Wetling in forum C Programming
    Replies: 7
    Last Post: 03-25-2002, 09:45 PM

Tags for this Thread