# Thread: probelms with using enter

1. ## 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:

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)
{
/*series*/

printf("R%i=" , ir); /*appends resistor number to R */
scanf("%f", &rinput);/*Resistor ohm value input; and overrides rinput=1 from declaration above */
printf("\n");
rtotal = rtotal + rinput;
ir = ir+1; /*increments the resistor R number by 1*/

}

case 2:

while(rinput == exit)

{
/*parallel*/

printf("R%i=", ir);
scanf("%f", &rinput);
printf("\n");
rrecip = rrecip+(1/rinput);
ir = ir+1;

printf("%f" , rrecip);

}

rtotal= 1/rrecip;

}

printf("%f", rtotal);

}```

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

Code:
```: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. Code:
```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.

4. Code:
`while(rinput== exit)`
rinput is a float, but exit() is a function from <stdlib.h>. You need to rethink that line.

Code:
`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.
Code:
`rtotal= 1/rrecip;`