LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 10-29-2004, 12:20 AM   #1
']['HeBroken
Member
 
Registered: Oct 2004
Posts: 35

Rep: Reputation: 15
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......
 
Old 10-29-2004, 12:33 AM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
scanf() is leaving a \n on the buffer. Try changing...
Code:
scanf("%c", &cCode);
...to...
Code:
scanf("%c", &cCode);
while(getchar() != '\n');
 
Old 10-29-2004, 12:36 AM   #3
']['HeBroken
Member
 
Registered: Oct 2004
Posts: 35

Original Poster
Rep: Reputation: 15
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..
 
Old 10-29-2004, 12:46 AM   #4
']['HeBroken
Member
 
Registered: Oct 2004
Posts: 35

Original Poster
Rep: Reputation: 15
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.
 
Old 10-29-2004, 01:42 AM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
while loop or for loop? mijohnst Programming 18 11-21-2005 04:48 PM
No loop# SeaSharp Linux - Newbie 3 09-06-2005 08:14 PM
while-loop Thomas23 Programming 4 05-24-2004 03:35 PM
for loop klfreese Programming 16 08-11-2003 11:09 AM
help with the following C loop ..... purpleburple Programming 12 08-06-2002 10:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration