Input problems?

This is a discussion on Input problems? within the C Programming forums, part of the General Programming Boards category; When I input, it just loops the text over and over, non stop. Code: #include <temp.h> #include <stdio.h> int main() ...

  1. #1
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218

    Input problems?

    When I input, it just loops the text over and over, non stop.

    Code:
    #include <temp.h>
    #include <stdio.h>
    
    int main()
    {
            float temp = 0.000;
            int choice;
            int loop = 1;
            while(loop == 1)
            {
                    printf("Temperature Conversions\nBy Samuel Fredrickson\n");
                    printf("\nTEMP: %f\n\n1. C2F\n2. F2C\n3. Exit\n",temp);
                    
    		scanf("%i",&choice);
    		scanf("%f",&temp);
    
    		switch(choice)
                    {
                            case 1:
                            temp = c2f(temp);
                            break;
    
                            case 2:
                            temp = f2c(temp);
                            break;
    
                            case 3:
                            return 0;
                    }
            }
            return 0;
    }

  2. #2
    Registered User terryrmcgowan's Avatar
    Join Date
    Jun 2003
    Posts
    8
    I am still a newbie, but it looks like your program sets the value of loop to 1 and then does

    while(loop ==1)
    {
    do stuff
    }

    but nothing in your program alters the value of loop so the condition is always true.

    either this might be helpful or I've missed the problem completely. (hope its the former)

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    12
    #include <temp.h>
    #include <stdio.h>

    int main()
    {
    float temp = 0.000;
    int choice;
    int loop = 1;
    while(loop == 1)
    {
    printf("Temperature Conversions\nBy Samuel Fredrickson\n");
    printf("\nTEMP: %f\n\n1. C2F\n2. F2C\n3. Exit\n",temp);

    scanf("%i",&choice);
    scanf("%f",&temp);

    switch(choice)
    {
    case 1:
    temp = c2f(temp);
    break;

    case 2:
    temp = f2c(temp);
    break;

    case 3: //CASE 3 WILL EXIT
    loop = 0;
    return 0;

    }
    }
    return 0;
    }

  4. #4
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218

    hmm

    The problem was just my compiler. I was using Pacific (DOS), but when I use gcc (DevC++(Win)) it works.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why don't you just set loop to zero instead of returning in case statement three? Many people like the "one exit from a function" method, meaning there is only one return statement in the entire function.

    I personally don't limit myself so, but commonly you can design with that in mind and still have clean, readable code.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help with C program writing
    By The_PC_Gamer in forum C Programming
    Replies: 9
    Last Post: 02-12-2008, 08:12 PM
  2. Problems with input and fflush
    By edugarcia in forum Linux Programming
    Replies: 1
    Last Post: 11-24-2004, 12:52 PM
  3. Custom Made Safe Input Function
    By Beast() in forum C Programming
    Replies: 6
    Last Post: 08-21-2004, 10:19 PM
  4. Input Problems and C# (CSharp) Tutorials
    By Grayson_Peddie in forum C# Programming
    Replies: 4
    Last Post: 02-27-2003, 09:45 PM
  5. Problems with commas as input
    By Yojimbo III in forum C++ Programming
    Replies: 3
    Last Post: 02-05-2003, 08:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21