Thread: probelms with using enter

  1. #1
    Registered User
    Join Date
    Jun 2007

    probelms with using enter

    I have written a program that accepts inputs for resistors in series and a program that will accept resistor values that are connected in parallel. On the menu to select series I used "1" and to select parallel I use "2". I did a printf on to make sure the math is applied correctly and indeed the formulas are correctly working.

    The problem I am having is escaping out of a loop using the enter key, when I am done entering resistor values. After getting out of the loop be pressing the enter key, it should display the final value. I then wanted to use enter again to get me back to the main option menu.

    Here is my pseudo code is:

    Begin display of option menu for series or parallel

    Enter values, when finished press enter to display final value

    To get back to the main option menu, press enter again

    Here is my actually code:

    *Written by: Dennis Reece
    #include <stdio.h>
    #include <math.h>
    int main(void)
    float rrecip=0; (reciprocal of R)
    int option;
    int ir = 1;  /* ir is the counter for R1.....Rn */
    float rinput=0, rtotal=0; /*rinput 	is the resistor value in ohms; and rinput is initially set to 1 to make the while
    statement true allowing the loop to function*/
    printf(" Type in 1 for seires or 2 for parallel\n ");
    /*scanf("%i", &rcount);
    for(ir=1; ir<=rcount; ir = ir+1)*/
    scanf("%c" , &option);
    switch (option)
    case 1:
    while(rinput== exit)
    printf("R%i=" , ir); /*appends resistor number to R */
    scanf("%f", &rinput);/*Resistor ohm value input; and overrides rinput=1 from declaration above */
    rtotal = rtotal + rinput;
    ir = ir+1; /*increments the resistor R number by 1*/
    case 2:
    while(rinput == exit)
    printf("R%i=", ir);
    scanf("%f", &rinput);
    rrecip = rrecip+(1/rinput);
    ir = ir+1;
    printf("%f" , rrecip);
    rtotal= 1/rrecip;
    printf("%f", rtotal);

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Your indenting sucks, and it makes your job as a programmer harder, but also those of us that have to go over your code.

    Compiling this for me results in the following errors:

    :25: error: `reciprocal' undeclared (first use in this function)
    :25: error: (Each undeclared identifier is reported only once
    :25: error: for each function it appears in.)
    :25: error: syntax error before "of"
    :40: error: `option' undeclared (first use in this function)
    :54: error: `exit' undeclared (first use in this function)
    You should fix these before working on your program logic, since these types of errors can create other problems seemingly related to other things.

    What it sounds like your problem is that you're describing is that when you read a char, it might be leaving the newline in the buffer. This means you need another simple getchar() present, but that's just a quick guess.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    char buff[BUFSIZ];
    while ( fgets( buff, sizeof buff, stdin ) != NULL ) {
      if ( buff[0] == '\n' ) break;  // user just pressed enter, so bail out
      if ( sscanf( buff, "&#37;c", &option ) == 1 ) {
        // and so on
    Use fgets() to read all input in the first instance, then use sscanf() (or anything else you like) to validate and convert what the user input.

    scanf() is fine as far as it goes, but as soon as the user wants to do something a bit extra, then it gets really messy in a hurry.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    while(rinput== exit)
    rinput is a float, but exit() is a function from <stdlib.h>. You need to rethink that line.

    float rrecip=0; (reciprocal of R)
    I assume you had a comment there . . .

    You have lots of places where you could get divide-by-zero errors. Here's one of them.
    rtotal= 1/rrecip;

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help With a BlackJack Program in C
    By Jp2009 in forum C Programming
    Replies: 15
    Last Post: 03-30-2009, 10:06 AM
  2. Assignment output help
    By Taka in forum C Programming
    Replies: 13
    Last Post: 09-23-2006, 11:40 PM
  3. endless loop for scanf - plz help
    By owi_just in forum C Programming
    Replies: 18
    Last Post: 03-20-2005, 01:41 PM
  4. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 PM
  5. hi need help with credit limit program
    By vaio256 in forum C++ Programming
    Replies: 4
    Last Post: 04-01-2003, 12:23 AM