GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Nov-2006, 23:39
NeXus NeXus is offline
New Member
 
Join Date: Oct 2006
Location: Sydney, Australia
Posts: 2
NeXus is on a distinguished road

Looping problem: Wont ask user to use program again


hello,

Ive recently coded this program in C#

CPP / C++ / C Code:
#include <stdio.h>
int main()
{
    char input, loop;
    float infar,cel,incel,far,inmil,kil,inkil,mil;
    printf("\t\t\tThis is a multi-tasking converter\t\t\t\n");
    do
    {
    printf("To convert Farenheit into Celcius press (f)\n");
    printf("To convert Celcius into Farenheit press (c)\n");
    printf("To convert Miles into Kilometres press (m)\n");
    printf("To convert Kilometres to Miles press (k)\n");
    scanf("%c", &input);
    if(input=='f')
        {
             printf("\t\t\tThis is a Farenheit to Celcius Converter\n");
             printf("Please enter the farenheit value:\n");
             scanf("%f", &infar);
             cel=(infar-32.0)*5.0/9.0;
             printf("%f celcius\n",cel);
        }
    else if(input=='c')
        {
             printf("\t\t\tThis is a Celcius to Farenheit Converter\n");
             printf("Please enter the Celcius value:\n");
             scanf("%f", &incel);
             far=(incel*1.8)+32.0;
             printf("%f farenheit\n",far);
        }
    else if(input=='m')
        {
             printf("\t\t\tThis is a Miles to Kilometres Converter\n");
             printf("Please enter a value in Miles:\n");
             scanf("%f", &inmil);
             kil=inmil/1.61;
             printf("%f kilometres\n",kil);
        }
    else if(input=='k')
        {
             printf("\t\t\tThis is the Kilometres to Miles Converter\n");
             printf("Please enter a value in Kilometres:\n");
             scanf("%f", &inkil);
             mil=inkil*1.61;
             printf("%f miles\n",mil);
        }
    else
        {
             printf("That is an invalid choice\n");
        }
printf("If you want to use the converter again press (y) or press (n) to quit\n");
scanf("%c",&loop);
}
while(loop!='n');
return 0;
}

what should it do: Its a program that converts distance and temperature

what is the problem:
The program loops without asking whether the user wants to use the program again by either pressing (y) for using it again or (n) to terminate the program.

It prints

If you want to use the converter again press (y) or press (n) to quit

but it doesnt get the users answer but instead just loops the program automatically without getting the users choice.

Can anyone suggest anything?

NeXus
Last edited by LuciWiz : 10-Nov-2006 at 01:58. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 10-Nov-2006, 00:47
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,373
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Looping problem: Wont ask user to use program again


When you input using
CPP / C++ / C Code:
scanf("%c", &input);
you press 2 keys, which means you have a dirty input stream after you read one character. This is a typical problem when entering single characters.

See this.
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
 
 

Recent GIDBlogVista ?Widgets? on Windows XP by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
Please Help Me To Build My Calendar!!! suriacute85 Java Forum 0 05-Oct-2006 19:39
Pipeline freeze simulation darklightred C++ Forum 6 27-Jul-2006 19:37
Runtime Problem involving "printf" in C Program supamakia C Programming Language 2 09-Oct-2005 10:09

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:39.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.