Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
10-29-2004, 12:20 AM
|
#1
|
|
Member
Registered: Oct 2004
Posts: 35
Rep:
|
WHILE LOOP in 'C'
ok here is my problem im trying to get this program to loop and loop and cant get it to. ok here is the code and im about to go crazy crazy crazy i dont know what im doing wrong. can anyone help me.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
/************************************************
\\ds
10.29.04
This Program Will Convert Farhenhiet to Centigrade & Vise Versa.
**************************************************/
char cCode [20];
int main()
{
int iStop = 0;
char cCode = 0;
float fCent, fFar, fDisplay;
while ( ! ( cCode == 1 ) )
{
printf("%69s ", ">><<>><< 1 /-\|\/| 7#3 1337357 /-\|>,()(_)|\||) <<>><<>><<> \n\n");
printf("\n %51s", " Temprature Conversion Program");
printf("\n\n %52s", "A. Farehnheit to Centigrade\n");
printf("\n %52s", "B. Centigrade to Farehnheit\n");
printf("\n %32s", "C. EXIT\n");
printf("\n\n %39s", "SELECT A/B/C>_");
scanf("%c", &cCode);
// STARTING THE IF STATEMENTS
if (cCode == 'A' || cCode == 'a')
{
printf(" Farehnheit Temprature ");
scanf("%f", &fFar);
fDisplay = (fFar -32)/1.8f;
{
printf("\nFarehnheit Temp (%.2f)\n\nCentigrade Converted Temp (%.2f)\n\n",fFar, fDisplay);
}
}
else
{
if (cCode == 'B' || cCode == 'b')
{
printf("Enter Cent Temp");
scanf("%f", &fCent);
fDisplay = (fCent * 1.8f) + 32;
{
printf("Centigrade Temprature (%.2f)\nConverted Farehnheit Temprature (%.2f)\n", fCent, fDisplay);
}
}
else
{
if (cCode == 'C' || cCode == 'c')
{
exit;
}
else
{
if (cCode != 'a','b','c','d')
{
printf("NOT VALID ENTRY\n");
}
}
}
}
}
cCode = 1;
return 0;
}
all i want the code to do is just loop over and over again... this is what i get when i run it....
Code:
>><<>><< 1 /-|/| 7#3 1337357 /-|>,()(_)|||) <<>><<>><<>
Temprature Conversion Program
A. Farehnheit to Centigrade
B. Centigrade to Farehnheit
C. EXIT
SELECT A/B/C>_a
Farehnheit Temprature 3.33
Farehnheit Temp (3.33)
Centigrade Converted Temp (-15.93)
>><<>><< 1 /-|/| 7#3 1337357 /-|>,()(_)|||) <<>><<>><<>
Temprature Conversion Program
A. Farehnheit to Centigrade
B. Centigrade to Farehnheit
C. EXIT
SELECT A/B/C>_NOT VALID ENTRY
>><<>><< 1 /-|/| 7#3 1337357 /-|>,()(_)|||) <<>><<>><<>
Temprature Conversion Program
A. Farehnheit to Centigrade
B. Centigrade to Farehnheit
C. EXIT
SELECT A/B/C>_
am i missing something? is it a stupid simple mistake that im missing or a huge ass mistake please help me thanx......
|
|
|
|
10-29-2004, 12:33 AM
|
#2
|
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
scanf() is leaving a \n on the buffer. Try changing...
Code:
scanf("%c", &cCode);
...to...
Code:
scanf("%c", &cCode);
while(getchar() != '\n');
|
|
|
|
10-29-2004, 12:36 AM
|
#3
|
|
Member
Registered: Oct 2004
Posts: 35
Original Poster
Rep:
|
YOU ARE FOOKING AWSOME !!!! thnx....... it works awsomely... now i just dont know how to clear the screen after it loops ... thanx :P :P :P im so excited..
|
|
|
|
10-29-2004, 12:46 AM
|
#4
|
|
Member
Registered: Oct 2004
Posts: 35
Original Poster
Rep:
|
oh yea one more question sorry for the hassle and bother....
ok now im using the switch statement and i get this warning
Code:
c:\documents and settings\bob\desktop\project 2\project 2.c(96) : warning C4700: local variable 'fFar' used without having been initialized
Last edited by ']['HeBroken; 10-29-2004 at 01:41 AM.
|
|
|
|
10-29-2004, 01:42 AM
|
#5
|
|
Moderator
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,801
|
printf("\033[H\033[J"); is clearing the screen with most terminal emulators, if you want a 100% portable solution look at curses.
the loop (and the program) should already stops with your test:
if (cCode == 'C' || cCode == 'c')
the construction:
if (cCode != 'a','b','c','d')
is certainly not doing what you expect (this test is always true).
Your program would greatly improve should you use the switch/case syntax vs all these ifs, and with a better care of the spelling, specially for the dialog with the user 
|
|
|
|
All times are GMT -5. The time now is 06:05 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|