LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   WHILE LOOP in 'C' (https://www.linuxquestions.org/questions/programming-9/while-loop-in-c-248637/)

']['HeBroken 10-29-2004 12:20 AM

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......

itsme86 10-29-2004 12:33 AM

scanf() is leaving a \n on the buffer. Try changing...
Code:

scanf("%c", &cCode);
...to...
Code:

scanf("%c", &cCode);
while(getchar() != '\n');


']['HeBroken 10-29-2004 12:36 AM

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..

']['HeBroken 10-29-2004 12:46 AM

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


jlliagre 10-29-2004 01:42 AM

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 :study:


All times are GMT -5. The time now is 09:15 AM.