I'm trying to create a function that asks the user for a value, which then will be stored as the max or min value entered, or if the user inputs a number < 0, it will exit the dataentry() function, else it will keep asking the user for input.
#include <stdio.h>
#include <string.h>
void dataentry();
int count = 0;
float max, min, mean, old, new;
float data;
char Old[10],Data[10],choice[25];
int main(void)
{
dataentry();
}
void dataentry()
{
printf(" |Enter Wind Speed Value:\n");
printf("**|Enter -1 to exit data entry mode|**\n");
fgets(Old, sizeof(Old), stdin);
sscanf(Old, "%f",&old);
max = old;
min = old;
data = 1;
count = 1;
printf("max=%f, min=%f, data=%f, count=%d.", max, min, data, count);
for (count == 1;data >= 0; count++)
{
printf("\nEnter data value: ");
//fgets(Data, sizeof(Data), stdin); // I commented this out because I got a coredump error with it in
sscanf(Data,"%f", &data);
if (data >= max)
{
max = data;
}
else if (data <= min && data > 0)
{
min = data;
}
}
}
After the program prompts you the first time to enter data, before it reaches the for loop, it works and you enter your value. Then however it goes into an infinite loop printing "Enter data value: " over and over. I used the printf statement that prints out the max, min, data, and count values so I could check that they are being stored and they are, but when the function gets to the for loop it no longer does what I'm trying to do. Thank you in advance, this function is part of a larger program I'm writing but I cut all the irrelevant stuff out.
If you uncomment your fgets() line, it ought to work. Here's a working version, tidied up a little to check the return from the functions that you aren't monitoring, and to improve the logic of your loop:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void dataentry(void);
int count = 0;
float max, min, mean, old, new;
float data;
char Old[10], Data[10], choice[25];
int main(void)
{
dataentry();
}
void dataentry(void)
{
printf(" |Enter Wind Speed Value:\n");
printf("**|Enter -1 to exit data entry mode|**\n");
if ( !fgets(Old, sizeof(Old), stdin) ) {
fprintf(stderr, "No input.\n");
exit(EXIT_FAILURE);
}
if ( sscanf(Old, "%f", &old) != 1 ) {
fprintf(stderr, "Badly formed input, enter a float next time.\n");
exit(EXIT_FAILURE);
}
max = old;
min = old;
data = 1;
count = 1;
printf("max=%f, min=%f, data=%f, count=%d.\n", max, min, data, count);
while (1) {
printf("Enter data value: ");
fflush(stdout);
if ( !fgets(Data, sizeof(Data), stdin) ) {
break;
}
if ( sscanf(Data, "%f", &data) != 1 ) {
fprintf(stderr, "Badly formed input, enter a float next time.\n");
exit(EXIT_FAILURE);
}
if ( data < 0 ) {
break;
}
else {
++count;
}
if ( data >= max ) {
max = data;
} else if ( data <= min ) {
min = data;
}
}
printf("max=%f, min=%f, data=%f, count=%d.\n", max, min, data, count);
}
with sample output:
paul#thoth:~/src/sandbox$ ./ssf
|Enter Wind Speed Value:
**|Enter -1 to exit data entry mode|**
10
max=10.000000, min=10.000000, data=1.000000, count=1.
Enter data value: 11
Enter data value: 12
Enter data value: 9
Enter data value: 8
Enter data value: -1
max=12.000000, min=8.000000, data=-1.000000, count=5.
paul#thoth:~/src/sandbox$
It's pretty bad form to use all those global variables, too. With your current program, at least, you could define every single one of them within dataentry().
Related
I write this program to do validation of selection (integer type variable) that entered by users. But the problem is after a valid input, the next invalid input (e.g: character type variable) will not be stored in the integer variable (selection). How can I solve this?
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#pragma warning (disable:4996)
void main()
{
int selection;
while (1)
{
while (1)
{
printf("Enter Your Selection (0-4) > ");
scanf("%d", &selection);
rewind(stdin);
if (!selectionCheck(&selection, 0, 4))
printf("Invalid\n");
else break;
}
printf("Success\n");
}
system("pause");
}
int selectionCheck(int *input, int min, int max)
{
char str[100] = "";
itoa(*input, str, 10);
if (isdigit(str[0]))
{
if (*input < min || *input > max)
return 0;
else return 1;
}
else
{
return 0;
}
}
Some notes:
1) You aren't checking the scanf() return value, and this is very usable: the negative return means that the entered characters can't be converted to int (because of "%d" format), and the return value equal 0 means that the input is empty (no characters entered).
2) In case that the user entered wrong character(s) (not digit(s)), the input buffer will remain busy until you read it in other way. Good idea is to use additional scanf("%s") here to read any characters as string, so buffer will be empty after this call. Using rewind() is not enough here.
3) There is no need to additional checking of input in selectionChecking() for isdigit(), because "%d" format in scanf() doesn't allow to read anything else but number.
4) There is no need to pass pointer to selection value in selectionChecking() call - it will be enough to pass it as value.
So, try this below:
// declaration of 'selectionCheck()'
int selectionCheck(int input, int min, int max);
void main()
{
int selection;
while (1)
{
while (1)
{
printf("Enter Your Selection (0-4) > ");
int ret = scanf("%d", &selection);
if (ret < 0) // invalid characters on input
{
printf("Invalid characters\n");
scanf("%s"); // empty buffer, reading it as string and putting readed characters to nowhere ;)
continue; // go to top of loop
}
if (ret == 0) // empty input
{
printf("No (empty) input\n");
continue; // go to top of loop
}
// here 'ret' is greather than 0, so valid number was entered
if (selectionCheck(selection, 0, 4)) // is value between 0 and 4 ?
break; // yes, success, break current loop!
printf("Invalid value\n");
}
printf("Success\n");
}
system("pause");
}
int selectionCheck(int input, int min, int max)
{
if (input < min || input > max)
return 0;
else
return 1;
}
Of course, you can write 'selectionCheck()' more condensed:
int selectionCheck(int input, int min, int max)
{
return (input < min || input > max) ? 0 : 1;
}
or simply:
int selectionCheck(int input, int min, int max)
{
return (input >= min && input <= max);
}
The function reads coefficients for the polynomial from standard input and stores them in the given array. The capacity parameter tells the function how much room the coeff[] array has for coefficients. The function tries to read all the coefficients it can until it reaches the end-of-file and returns the number of coefficients it actually reads. If the input polynomial is bad (e.g., with too many coefficients or with input that doesn't parse as a floating point number), this function will print "Invalid polynomial" and exit the program with a status of 101.
The input file is like this:
0.0 6.0
25.00 -47.50 25.17 -5.00 0.33
The first two numbers is the range of the plot and the second line represents the coefficients of the polynomial.
This is the code I have so far:
/**
*/
// Include our own header first
#include "poly.h"
// Then, anything else we need in the implementation file.
#include <stdlib.h>
#include <stdio.h>
/** Exit status if the input polynomail is bad. */
#define INVALID_POLYNOMAIL_STATUS 101
int readPoly( int capacity, double coeff[] )
{
double variable = 0.0;
int ch;
int count = 0;
while ( ( ch = getchar() ) != EOF ) {
for(int i = 0; i < capacity; i++) {
if(scanf("%lf", &variable) != 1) {
fprintf(stderr, "Invalid input");
exit(101);
}
else {
coeff[i] = variable;
count++;
}
}
}
return count;
}
getchar may read the beginning of a value, this is not correct like that
A simple way is to stop on any error (EOF or bad value) :
int readPoly( int capacity, double coeff[] )
{
int i;
for(i = 0; i < capacity; i++) {
if (scanf("%lf", &coeff[i]) != 1)
break;
}
return i;
}
An other way is to bypass by hand the spaces to be able to indicate an error :
int readPoly( int capacity, double coeff[] )
{
int i;
for (i = 0; i < capacity; i++) {
for (;;) {
int c;
if ((c = getchar()) == EOF)
return i;
if (!isspace(c)) {
ungetc(c, stdin);
break;
}
if (scanf("%lf", &coeff[i]) != 1) {
fprintf(stderr, "Invalid input");
exit(101);
}
}
return i;
}
Note count is redundant with i, just i is enough, and you can also directly scanf into the array
writing a program that will be finding min, max, avg of values entered by user. Having trouble writing something that will check to make sure there are only postive integers entered and produce an error message. heres my for statement that is reading the input so far:
for (int value = 0; value <= numofvals; ++value) {
printf("Value %d: %f\n", value, val_input);
scanf("%f", &val_input);
}
mind you I've been learning code for about 3 weeks and was just introduced to loops this week so my understanding is rudimentary at best!
First, don't use scanf. If stdin doesn't match what it expects it will leave it in the buffer and just keep rereading the same wrong input. It's very frustrating to debug.
const int max_values = 10;
for (int i = 0; i <= max_values; i++) {
int value;
if( scanf("%d", &value) == 1 ) {
printf("Got %d\n", value);
}
else {
fprintf(stderr, "I don't recognize that as a number.\n");
}
}
Watch what happens when you feed it something that isn't a number. It just keeps trying to read the bad line over and over again.
$ ./test
1
Got 1
2
Got 2
3
Got 3
foo
I don't recognize that as a number.
I don't recognize that as a number.
I don't recognize that as a number.
I don't recognize that as a number.
I don't recognize that as a number.
I don't recognize that as a number.
I don't recognize that as a number.
I don't recognize that as a number.
Instead, use fgets to reliably read the whole line and sscanf to parse it. %f is for floats, decimal numbers. Use %d to recognize only integers. Then check if it's positive.
#include <stdio.h>
int main() {
const size_t max_values = 10;
int values[max_values];
char buf[1024];
size_t i = 0;
while(
// Keep reading until we have enough values.
(i < max_values) &&
// Read the line, but stop if there's no more input.
(fgets(buf, sizeof(buf), stdin) != NULL)
) {
int value;
// Parse the line as an integer.
// If it doesn't parse, tell the user and skip to the next line.
if( sscanf(buf, "%d", &value) != 1 ) {
fprintf(stderr, "I don't recognize that as a number.\n");
continue;
}
// Check if it's a positive integer.
// If it isn't, tell the user and skip to the next line.
if( value < 0 ) {
fprintf(stderr, "Only positive integers, please.\n");
continue;
}
// We got this far, it must be a positive integer!
// Assign it and increment our position in the array.
values[i] = value;
i++;
}
// Print the array.
for( i = 0; i < max_values; i++ ) {
printf("%d\n", values[i]);
}
}
Note that because the user might input bad values we can't use a simple for loop. Instead we loop until either we've read enough valid values, or there's no more input.
Something easy like this may work for you:
int n;
int ret;
for (;;) {
ret = scanf("%d", &n);
if (ret == EOF)
break;
if (ret != 1) {
puts("Not an integer");
for (;;)
if (getchar() == '\n')
break;
continue;
}
if (n < 0) {
puts("Not a positive integer");
continue;
}
printf("Correct value %d\n", n);
/* Do your min/max/avg calculation */
}
/* Print your results here */
This is just an example and assumes you do not need to read floating point numbers and then check if they are integers, as well as a few other things. But for starters, it is simple and you can work on top of it.
To break out of the loop, you need to pass EOF (typically Ctrl+D in Linux/macOS terminals, Ctrl+Z in Windows ones).
An easy and portable solution
#include <limits.h>
#include <stdio.h>
int get_positive_number() {
char buff[1024];
int value, ch;
while (1) {
printf("Enter positive number: ");
if (fgets(buff, 1023, stdin) == NULL) {
printf("Incorrect Input\n");
// Portable way to empty input buffer
while ((ch = getchar()) != '\n' && ch != EOF)
;
continue;
}
if (sscanf(buff, "%d", &value) != 1 || value < 0) {
printf("Please enter a valid input\n");
} else {
break;
}
}
return value;
}
void solution() {
// Handling malformed input
// Memory Efficient (without using array to store values)
int n;
int min = INT_MAX;
int max = INT_MIN;
double avg = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
getc(stdin);
int value;
for (int i = 0; i < n; i++) {
value = get_positive_number();
if (value > 0) {
if (min > value) {
min = value;
}
if (max < value) {
max = value;
}
avg += value;
}
}
avg = avg / n;
printf("Min = %d\nMax = %d\nAverage = %lf\n", min, max, avg);
}
int main() {
solution();
return 0;
}
Output:
Enter number of elements: 3
Enter positive number: 1
Enter positive number: 2
Enter positive number: a
Please enter a valid input
Enter positive number: -1
Please enter a valid input
Enter positive number: 1
Min = 1
Max = 2
Average = 1.333333
Yes, hello. I'm really new to programming. I genuinely need help because I would like to both understand what I'm doing wrong and also pass my class.
I'm in a Intro to Programming class and my assignment is to create a program that uses an array to store percentage grades in the range from 0 to 100 (inclusive). The program should allow the user to indicate when they are done entering grades. When the user is done entering grades, the program should print out the grades entered.
I have a running code, compiled in Code::Blocks. But my questions/problems are:
A.) What's the best way to tell C when the user is done? Should I
Leave the code as is and let any key be hit?
Add a specific variable like 'done'?
Do something else?
B.) How do I print only the grades that have been entered without it going through all 100 slots? I can't for the life of me find the solution.
Any and all suggestions welcome and appreciated!!!
int i = 0;
float percentScore[100];
for (i = 0; i < 10; i++) {
printf("Grade %d: ", i + 1);
scanf("%f", &percentScore[i]);
}
for (i = 0; i < 10; i++) {
printf("\n%.2f%%", percentScore[i]);
}
return 0;
Your choices for A) are not mutually exclusive; the first is something the user can do, the second is a way to represent that in the code. As such, you could reasonably do both.
As for B), you need a way to represent the number of grades entered (hint: a variable); this can then be used to control how many get printed.
Here is a simple approach for your problem:
for each input, read a full line from the user ;
try and convert this line as a number with sscanf() ;
if the conversion fails, consider the input exhausted ;
if the value is out of bounds, restart this input.
The user can thus signal the end of the list by entering a blank line.
Here is a example:
#include <stdio.h>
#define GRADE_NB 100
int main(void) {
int i, n;
float grade, percentScore[GRADE_NB];
char buf[100];
for (n = 0; n < GRADE_NB;) {
printf("Grade %d: ", n + 1);
if (!fgets(buf, sizeof buf, stdin)) {
/* end of file reached: input exhausted */
break;
}
if (sscanf(buf, "%f", &grade) != 1) {
/* not a number: input exhausted */
break;
}
if (grade >= 0 && grade <= 100) {
/* value is OK, grade accepted */
percentScore[n] = grade;
n++;
} else {
printf("Invalid grade: %f\n", grade);
}
}
for (i = 0; i < n; i++) {
printf("%.2f%%\n", percentScore[i]);
}
return 0;
}
If the potential number of inputs is not limited, you must allocate the array from the heap and reallocate it as more inputs are gathered. Here is a simple solution for this where the array is reallocated for each input:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int i, n;
float grade, *percentScore;
char buf[100];
for (n = 0, percentScore = NULL;;) {
printf("Grade %d: ", n + 1);
if (!fgets(buf, sizeof buf, stdin)) {
/* end of file reached: input exhausted */
break;
}
if (sscanf(buf, "%f", &grade) != 1) {
/* not a number: input exhausted */
break;
}
if (grade >= 0 && grade <= 100) {
/* value is OK, grade accepted */
percentScore = realloc(percentScore, (n + 1) * sizeof(*percentScore));
if (percentScore == NULL) {
printf("allocation failed\n");
return 1;
}
percentScore[n] = grade;
n++;
} else {
printf("Invalid grade: %f\n", grade);
}
}
for (i = 0; i < n; i++) {
printf("%.2f%%\n", percentScore[i]);
}
free(percentScore);
return 0;
}
I got a data file which consists of a single column with the header name with temperature and the following rows are just a series of recorded temperature. I can read it successfully(perhaps) into the C program using the following command:
#include <stdio.h>
#include <cstdlib>
int main()
{
FILE *fpt; /*define a pointer to predefined structure type FILE*/
fpt = fopen("temperature.dat","r");
char temp[10];
float t[7];
int i;
fscanf(fpt, "%s",temp);
printf("%s",temp);
for(i=0;i<7;++i)
{
fscanf(fpt, "%f",&t[i]);
printf("%.2f",t[i]);
}
printf("%f",t[3]); /*just testing whether the program is reading correctly*/
fclose(fpt);
system("pause");
}
But the problem is how could I detect when there is a series of temperature, for instance 6 temperature values are increasing continuously. I need something like IF total of 6 values of temperature is increased continuously, then it will generate some error message using printf function. Assume that the total input number of data is not fixed, how could I program it.
There is no need to use an extra loop. You can just do
totalInc = 0;
for(i=0;i<7;++i) {
fscanf(fpt, "%f",&t[i]);
printf("%.2f",t[i]);
if (i > 0) {
if (t[i] > t[i-1]) totalInc += 1;
else totalInc -= 1;
}
}
The totalInc will tell you the number of times the current value is greater than the previous value. For your case, you can then just check for totalInc == 6 but really, you can just check for any number of increments. A positive number will indicate a general incremental trend, while a negative number will indicate a general decreasing trend.
To detect whether a file of floats has at least 6 increasing values in a row, you could do something like this:
#include <stdio.h>
#define IN_A_ROW 6
int main() {
FILE *f = fopen("temps.txt", "r");
float x, last_x;
int inc = 0;
fscanf(f, "%f", &last_x);
while (fscanf(f, "%f", &x) == 1) {
if (x > last_x) { // or maybe >=
if (++inc >= IN_A_ROW) {
printf("Found %d increases in a row\n", IN_A_ROW);
return -1;
}
}else
inc = 0;
last_x = x;
}
fclose(f);
return 0;
}
Add a variable (say, inctemp) to count seeing increases in a row, and increment it in your loop if there is an increase. Reset it to 0 if there is not an increase. At the end of your loop, you know how many in a row there were (at least at the end of the data set)
Modified for arbitrary number of reads
int inctemp = 0;
float curtemp, prevtemp;
...
if ( fscanf(fpt, "%f",&prevtemp) == 1)
printf("%.2f",prevtemp);
while( fscanf(fpt, "%f",&curtemp) == 1)
{
printf("%.2f",curtemp);
if( curtemp > prevtemp ) {
inctemp++;
}
else {
inctemp = 0;
}
if( inctemp == 6 ) {
printf("Six increases in a row!\n");
}
prevtemp = curtemp;
}
}
Finding a delta between the temperatures will help you.
#include <stdio.h>
#include <cstdlib>
int main()
{
FILE *fpt; /*define a pointer to predefined structure type FILE*/
fpt = fopen("temperature.dat","r");
char temp[10];
float t[7];
int i, loweringdelta;
fscanf(fpt, "%s",temp);
printf("%s",temp);
loweringdelta = 1;
for (i=0; i<7; ++i)
{
fscanf(fpt, "%f", &t[i]);
printf("%.2f", t[i]);
if (i > 0 && (t[i]-t[i-1]<= 0))
{
loweringdelta = t[i]-t[i-1];
}
}
if (loweringdelta > 0)
{
// Your error message here
}
printf("%f", t[3]); /*just testing whether the program is reading correctly*/
fclose(fpt);
system("pause");
}
You will need some kind of counter to see how many times you have seen incrementing temperatures. Also, read the file in a while loop:
#include <stdio.h>
int main()
{
FILE *fpt; /*define a pointer to predefined structure type FILE*/
fpt = fopen("temperature.dat","r");
char temp[10];
int count = 0;
int i;
float prev_temp = -999.00;
float current_temp;
int threshold = 6;
fscanf(fpt, "%s",temp); // header?
printf("Header: %s\n",temp);
while(!feof(fpt)) {
fscanf(fpt, "%f", ¤t_temp);
if (current_temp > prev_temp) count++;
else count = 0;
prev_temp = current_temp;
if (count > threshold) printf("Saw %d consecutive increases\n", count);
}
fclose(fpt);
}