473,407 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Problem with a while loop...

I am trying to write a program using a while loop, and a nested for loop inside. The program needs to run once, and then after completion, ask if the user would like to run the program again. The while statement needs to be true while the reply is "y" and must clear the buffer afterwards (i.e. must accept "yes", "youbetcha" etc. and clear the buffer after. ) Im stuck, the program is compiling, but returns to prompt when I run it. Here is the code:

Expand|Select|Wrap|Line Numbers
  1. #include <math.h>
  2. #include <stdio.h>
  3.  
  4. main ()
  5.  
  6. {
  7.  
  8.  
  9.      fputs("This program will calculate a table showing the voltage,"
  10.            "total resistance, and current for each increment of "
  11.            "resistance given the input from the user\n\n", stdin);
  12.  
  13.      char y;
  14.      int reply, ch;
  15.      double initcur, endcur, inccur, voltage, totresist, current;
  16.  
  17.        reply = y;
  18.        while ( reply == 'y' )
  19.  
  20.  
  21.      {
  22.          fputs("Enter voltage: ", stdin);
  23.          scanf("%f", &voltage);
  24.          fputs("Enter initial resistance value: ", stdin);
  25.          scanf("%f", &initcur);
  26.          fputs("Enter ending resistance value: ", stdin);
  27.          scanf("%f", &endcur);
  28.          fputs("Enter incremental resistance value: ", stdin);
  29.          scanf("%f", &inccur);
  30.  
  31.          fputs("Table of induced currents\n\n", stdin);
  32.          fputs("     Voltage     Total Resistance     Current\n\n",
  33. stdin);
  34.          fputs("     ----------------------------------------\n\n",
  35. stdin);
  36.  
  37.  
  38.          for (initcur ; initcur<=endcur ; initcur + inccur)
  39.          {
  40.            current = voltage / (initcur);
  41.  
  42.            printf("     %2if     %4if     %4if\n", voltage, totresist,
  43.                  current);
  44.           }
  45.  
  46.        ch = getc (stdin);
  47.  
  48.        fputs("Would you like another computation? ", stdin);
  49.        scanf("%c", &reply);
  50.  
  51.        return 0;
  52.       }
  53. }
  54.  
Any help would be appreciated! :)
Feb 12 '07 #1
4 2360
RRick
463 Expert 256MB
You've got your return 0 inside the while loop. This is what is causes to program to stop after just one iteration.

Move the return just past the while loop and things should improve.
Feb 12 '07 #2
Ganon11
3,652 Expert 2GB
Further, when initializing reply, you set it equal to y, a char variable. But you never initialized y - thus, reply holds some garbage value. You should set reply equal to the character value 'y' and get rid of the y variable.
Feb 13 '07 #3
Ok so I rewrote your code abit. However, Now I came up with my own problem (im fairly new to c programming). I used a switch case within a while loop to ask the user if they want to perform the program again, but whenever they enter anything, the program simply exits. Hopefully someone can figure out why because I don't see why it would do that.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <conio.h>
  3.  
  4. int main (){
  5.  
  6.          char choice;
  7.          float initcur, endcur, inccur, voltage, totresist, current;
  8.  
  9.          printf("This program will calculate a table showing\n"
  10.          "the voltage total resistance, and current for each\n"
  11.          "increment of resistance given the input from the user\n\n");
  12.          printf("Would you like to run the program? ");
  13.          scanf("%c", &choice);
  14.  
  15.  
  16.          while (choice=='y'){
  17.  
  18.            switch(choice){
  19.              case 'y': 
  20.                   printf("\n\nEnter voltage: ");
  21.                   scanf("%f", &voltage);
  22.                   printf("Enter initial resistance value: ");
  23.                   scanf("%f", &initcur);
  24.                   printf("Enter ending resistance value: ");
  25.                   scanf("%f", &endcur);
  26.                   printf("Enter incremental resistance value: ");
  27.                   scanf("%f", &inccur);
  28.  
  29.                   printf("Table of induced currents\n\n");
  30.                   printf("      Voltage    Total Resistance    Current\n\n");
  31.                   printf("----------------------------------------\n\n");
  32.                   /*
  33.                   for (initcur; initcur<=endcur; initcur=initcur+inccur)
  34.                   {
  35.                    current = voltage/initcur;
  36.                    printf("      %f         %f%             f%", voltage, totresist, current);
  37.                    printf("\n");
  38.                   }*/
  39.  
  40.                     do{
  41.                     current = voltage/initcur;
  42.                     printf("      %f         %f%                   f%", voltage, initcur, current);
  43.                     printf("\n");
  44.                     initcur=initcur+inccur;
  45.                     }         
  46.                     while(initcur<=endcur);
  47.  
  48.                printf("\nWould you like to perform another computation?  ");
  49.                scanf("%c", &choice);
  50.                break;
  51.  
  52.                default:
  53.                     {
  54.                      printf("Is this the problem?");
  55.                     }
  56.  
  57.                }
  58.             }
  59.     getch();
  60.   }
  61.  
Apr 20 '11 #4
the default should actually be return(0);, i just put that statement there to see if the return was the reason for the error.
Apr 20 '11 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Charles Alexander | last post by:
Hello I am new to php & MySQL - I am trying to retrieve some records from a MySQL table and redisplay them. The data in list form looks like this: Sample_ID Marker_ID Variation ...
4
by: JP SIngh | last post by:
Thanks to Manohar for writing the basic code for displaying the managers and the employees in a tree like structure. I have adapted the code below but it gives me an error "exception occcured"...
2
by: Xiaozhu | last post by:
I trid to use icc -axW to optimize the code, but it becomes much slower... 8sec/37sec before and after using this option. my cpu is Intel(R) Pentium(R) 4 CPU 2.40GHz the test code is just one...
5
by: Carmine Cairo | last post by:
Hi, I'm working on a project and today I've note a little problem during the compile fase. Here a little piece of code: // 1st version welldone = 0; size = p->getSize(); backbone = new...
8
by: Jeff | last post by:
Hello everybody, I was doing one of the exercises in the K&R book, and I got something really strange. Here's the source code: /* * Exercise 2-2 from the K&R book, page 42 */ #include...
47
by: fb | last post by:
Hi Everyone. Thanks for the help with the qudratic equation problem...I didn't think about actually doing the math...whoops. Anyway... I'm having some trouble getting the following program to...
8
by: koorb | last post by:
I am starting a program from a module with the Sub main procedure and I want it to display two forms for the program's interface, but when I run the program both forms just open and then program...
11
by: felixnielsen | last post by:
What i have is not even a real problem, but i hope someone can help anyway, but first a piece of code_ @code start const unsigned short Size = 2; // 2^N std::bitset<Size*Size*Size> YZ;...
1
by: Promextheus Xex | last post by:
First php post... Hi there, I'm writing some code for my website to list people listening to my music. I have a loop for an array that dosen't seem to display all people. most of the time it...
5
by: =?Utf-8?B?Z215ZXJz?= | last post by:
Hello, I am attempting to start a cmd.exe process and pass several .vbs scripts (with additional parameters) and then read the output from the scripts and make "notes" in a DataTable (the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.