Double usage of scanf() depends on call order - c

I wrote a program in C which takes as an input a value and an ordered Array of integers and performs a ternary search to find the value(if it exists) inside the Array.
I have seen all the possible problems with the usage of scanf and the related topics here in Stackoverflow.
I have noticed that there is a difference if I call the 2 scanf functions in reverse order.
If I use the code as it is below. First read the value and after the array from the user, the program and scanf functions as expected.
printf("Enter the value to be searched in the Array: ");
int k;
scanf(" %d", &k);
printf("Type elements of A(sorted) separated by spaces (type 'end' to stop): ");
i = 0;
while(scanf("%d", &A[i]) == 1) {
i++;
}//while
Although if I use the scanf inputs in the reverse order the second scanf never stops to get user input and read values left in the buffer.
printf("Type elements of A(sorted) separated by spaces (type 'end' to stop): ");
i = 0;
while(scanf("%d", &A[i]) == 1) {
i++;
}//while
printf("Enter the value to be searched in the Array: ");
int k;
scanf(" %d", &k);
I cannot understand what is the difference in the calling order.
I have tried the solutions mentioned in the other threads but none worked.
Just as a reference here is the whole code(working as expected):
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int ternarySearch(int A[], int l, int r, int k){
int i;
int first,second;
if(l>r){
return -1;
}
i= (r - l)/3;
if(i==0){
i++;
}
first = i+l-1;
second = i*2+l-1;
if(A[first]==k){
return first;
}
else if(A[first]>k){
ternarySearch(A, l, first-1, k);
}
else
{
if(A[second]==k)
return second;
else
if(A[second]>k)
ternarySearch(A, first+1,second-1, k);
else
ternarySearch(A, second+1,r, k);
}
}
int main(){
const int maxarraylen = 1000;
int i;
int n;
int A[maxarraylen];
char string[250];
printf("Enter the value to be searched in the Array: ");
int k;
scanf(" %d", &k);
printf("Type elements of A(sorted) separated by spaces (type 'end' to stop): ");
i = 0;
while(scanf("%d", &A[i]) == 1) {
i++;
}//while
n=i-1;
//We assume the array is sorted otherwise we can use any sorting algorithm e.g. code from task1
scanf(" %d", &k);
int result;
result=ternarySearch(A, 0, n, k);
if(result==-1){
printf("The value was not found in the Array.\n");
}
else{
printf("The value was found in position no. %d.\n", result);
}
return 0;
}

Your problem is that you are not 'stepping over' your end input.
We can see this by doing an experiment using the following program:
#include <stdio.h>
#include <stdlib.h>
void main(void) {
FILE *f;
long f_pos;
int ret;
int i;
int data[5];
int data_last;
int search;
f = fopen("./input.txt", "r");
if (f == NULL) {
perror("fopen()");
return;
}
/* read in the values for the array */
data_last = -1;
for (i = 0; i < 5; i++) {
ret = fscanf(f, "%d", &(data[i]));
printf("fscanf(data[%d]): ret: %d\n", i, ret);
f_pos = ftell(f);
printf("ftell(): %ld\n", f_pos);
if (ret != 1) {
break;
}
data_last = i;
}
/* check that we read in at least one value */
if (data_last == -1) {
printf("no input data!\n");
return;
}
/* insert 'fix' here */
/* pre-load the 'search' with known garbage */
search = 987;
/* now read in the search value */
ret = fscanf(f, "%d", &search);
printf("fscanf(search): ret: %d\n", ret);
f_pos = ftell(f);
printf("ftell(): %ld\n", f_pos);
/* print out our info */
for (i = 0; i <= data_last; i++) {
printf("data[%d]: %d\n", i, data[i]);
}
printf("search for: %d\n", search);
return;
}
With the following data in input.txt:
123
456
end
456
The output is as follows:
fscanf(data[0]): ret: 1
ftell(): 3
fscanf(data[1]): ret: 1
ftell(): 7
fscanf(data[2]): ret: 0
ftell(): 8
fscanf(search): ret: 0
ftell(): 8
data[0]: 123
data[1]: 456
search for: 987
ftell() tells us where the file's cursor is, and in this case we can see that it is at byte 8... the e of the input line end.
It doesn't get past it, and thus the next attempt to read a number (%d) will fail too!
It's also a good idea to check the return values! We can see that the fscanf(&search) call has failed to read a number!
The solution is to insert this snippet just after we check that we recieved array values:
/* this is the 'fix' */
ret = fscanf(f, "end");
printf("fscanf(end): ret: %d\n", ret);
f_pos = ftell(f);
printf("ftell(): %ld\n", f_pos);

Related

How to fix 'Segmentation fault' while redirecting input in C

I've searched for solutions to that problem and couldn't find any that match mine.
I wrote a program that gets two arrays of integers and return the scalar product between them. It works fine when I'm submitting the input manually, but when I try to read the input from a text file, I encounter that Segmentation fault.
Edit: I'm talking about stdin redirection
I would be grateful for some help.
The code is:
#include <stdio.h>
#define MAXLIMIT 100
int scalar_product(int[], int[], int);
void set_array(int[]);
int main(){
int arr1[MAXLIMIT], arr2[MAXLIMIT];
int size, result;
set_array(arr1);
set_array(arr2);
printf("Enter the vectors' dimension: ");
scanf("%d", &size);
result = scalar_product(arr1, arr2, size);
printf("The scalar product is: %d \n", result);
return 0;
}
void set_array(int a[]){
int i;
printf("Please enter a vector with up to %d elements: \n", MAXLIMIT);
for (i = 0; i < MAXLIMIT - 1 && (scanf("%d", &a[i]) != EOF); i++);
}
int scalar_product(int a1[], int a2[], int size){
int product = 0, i;
for (i = 0; i < size; i++){
product += a1[i] * a2[i];
}
return product;
}
and the text file contains:
1 -2 3 -4
6 7 1 -2
4
HEre
void set_array(int a[]) {
int i;
printf("Please enter a vector with up to %d elements: \n", MAXLIMIT);
for (i = 0; i < MAXLIMIT - 1 && (scanf("%d", &a[i]) != EOF); i++);
}
When reading from the console you will never hit EOF (unless you enter ctrl-D which I guess you didnt) so your set_array loops just keep going, reading from the file. You read all the data in the first set_array and read nothing in the second one because you have finished the input file
the actualk failure was that you ran off the end of the file, so the scanf of size failed and you were trying to read a random sized array in the function scalar_product.
Test the return from scanf always
What you need to do is put a count in the file before the first array so you know how many items to read into arr1 and I suggest a count before the second lot too.
ie
void set_array(int a[]) {
int i;
int count = 0;
printf("Please enter how many elements you want to enter, max = %d \n", MAXLIMIT);
scanf("%d", &count);
if(count > MAXLIMIT) count = MAXLIMIT;
for (i = 0; i < count && (scanf("%d", &a[i]) != EOF); i++);
}

segmentation fault (core dumped) gcc ubuntu

I was trying to make a simple function to make a group of number that user enters them, using pointer of pointer but i keep getting this error and its hard to tell where the problem is, if there any other option to use something else that tells me where the problem is instead of this weird error.
#include <stdio.h>
void BuildGroub(int** group,int* count){
int i=0;
int j;
printf("Enter the size of the group \n");
scanf("%d", &*count);
while(*count != 0){
printf("Enter the %d number of the group:\n", i);
j=0;
scanf("%d", &**(group+i));
while(**(group+i)!=**(group+j)){
j++;
}
if(j==i){
i++;
count--;
} else{
printf("you have already entered this number please try again: \n");
}
}
}
int main(){
int count;
int group[100];
int *groupptr = &group;
BuildGroub(&groupptr,&count);
for(int i=0;i<count;i++){
printf("%d, ", group[i]);
}
return 0;
}
With this question, you do not need to use double pointer. If you want to learn how to use the double pointer, you can google then there are a ton of examples for you, for example, Double Pointer (Pointer to Pointer) in C.
In BuildGroub you decrease the count pointer
if(j==i){
i++;
count--;
}
, but in the condition of while loop, you compare the value that count pointer points to. it seems strange.
while(*count != 0)
Even if you change count-- to (*count)--, it will decrease the number of elements that you enter to 0 when you get out of the while loop, then in main function:
for(int i=0;i<count;i++){} // count is equal to 0 after calling BuildGroub function if you use (*count--) in while loop.
You should use a temp value for while loop function, for example:
int size = *count;
while(size != 0){
...
if (i == j) {
i++;
size--;
}
}
You should use, for example, group[i] instead of *(group+i). It will be easier to read your code.
The code complete:
#include <stdio.h>
void BuildGroub(int* group,int* count){
int i=0;
int j;
printf("Enter the size of the group \n");
scanf("%d", count);
int size = *count;
while(size != 0){
printf("Enter the %d_th number of the group:\n", i);
j=0;
scanf("%d", &group[i]);
while(group[i] != group[j]) {
j++;
}
if(j==i){
i++;
size--;
} else{
printf("you have already entered this number please try again: \n");
}
}
}
int main(){
int count;
int group[100];
int *groupptr = group;
BuildGroub(groupptr,&count);
for(int i=0;i<count;i++){
printf("%d, ", group[i]);
}
return 0;
}
The test:
./test
Enter the size of the group
5
Enter the 0_th number of the group:
1
Enter the 1_th number of the group:
2
Enter the 2_th number of the group:
2
you have already entered this number please try again:
Enter the 2_th number of the group:
3
Enter the 3_th number of the group:
3
you have already entered this number please try again:
Enter the 3_th number of the group:
4
Enter the 4_th number of the group:
5
1, 2, 3, 4, 5,
If you want to use a double pointer, you need to change your function like this:
void BuildGroub(int** group, int* count) {
int i = 0;
int j;
printf("Enter the size of the group \n");
scanf("%d", &*count); //I think this is redundant but works.
while (*count != 0) {
printf("Enter the %d number of the group:\n", i);
j = 0;
scanf("%d", (*group + i)); //The content of group + i
while ( *( *group + i) != *(*group + j)) { //the content of the content
j++;
}
if (j == i) {
i++;
(*count)--; //The content decrement
} else {
printf("you have already entered this number please try again: \n");
}
}
}
But you have a big problem in main and it is because you are using the parameter count to decrement until zero inside the function. So when the function finish, count value is zero and you don't print anything... You need to change this, using a internal variable to make the count, and finaly, setting the parameter to be using in main.

Can't initialize my array counter in C despite trying everything

I've been working on this school assignment which creates a restaurant menu, and I have an array to store everything that a person orders. The array that stores the orders ordered is array OrderedItems [30]. This array is basically a counter. When a person orders order number 1 as an example OrderItems[1] increases by 1.
I've tried to initialize by using OrderedItems [30] = {0}, and using a for loop to initialize every spot individually, However, that didn't work. Please help me initialize this array. I've also tried memset(OrderedItems, 0, 30); and this didn't work too so I really have no clue what to do.
I also want to add that I've tried to globally declare the OrderedItems array since I've heard that all global declarations are automatically initialized to 0, but that also didn't work.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <windows.h>
FILE *fPointer1; //for food//
FILE *fPointer2; //for invoice//
int count;
char name[50];
float price;
void FunctionToPrintFoodItems (void)
{
fPointer1 = fopen ("Food.txt", "a+");
float price;
printf("ITEMCODE\tDESCIPTION\tPRICE (RM)\n");
while (!feof (fPointer1))
{
fscanf(fPointer1, "%d %s %f", &count, name, &price);
printf("%d\t\t%s\t\t%.2f\n", count, name, price);
}
fclose (fPointer1);
}
void clrscr()
{
system("#cls||clear"); //might not need. Will delete it not needed//
}
void debug (void)
{
printf("THIS IS PRINTED");
}
#define clear clrscr ();
#define printfood FunctionToPrintFoodItems();
#define de debug();
int main ()
{
fPointer1 = fopen("Food.txt", "w");
fprintf(fPointer1, "1 BigM 10.40\n");
fprintf(fPointer1, "2 Cheeseburger 9.45");
fclose (fPointer1);
int i;
int MainMenuCode;
int additems = 0;
int orderitems;
int item;
int ordered;
int OrderedItems[30] = {0};
memset(OrderedItems, 0, 30);
for (i=0 ; i < 30 ; i++)
{
OrderedItems[i] = NULL;
printf("%d: %d\n", i, OrderedItems);
}
do
{
printf("WELCOME TO RESTOURANT MAC C - Main Menu\n\n");
printf("[1] Add new food items\n\n");
printf("[2] Take order\n\n");
printf("Enter ITEM CODE (0 to Quit) : ");
scanf("%d", &MainMenuCode);
if (MainMenuCode == 1)
{
clear;
additems = 1;
printf("WELCOME TO RESTAURANT MAC C - Add Menu\n\n");
printfood;
printf("\n");
while ( additems == 1 )
{
printf("Enter description (0 to Main Menu) : ");
scanf("%s", name);
if (strcmp (name, "0") == 0)
{
additems = 0;
clear;
break;
}
printf("Enter price (RM) : ");
scanf("%f", &price);
count ++;
fPointer1 = fopen ("Food.txt", "a");
printf("\n%d\t\t%s\t\t%.2f\n\n", count, name, price);
fprintf(fPointer1, "\n%d %s %.2f", count, name, price);
fclose (fPointer1);
}
}
else if (MainMenuCode == 2)
{
clear;
orderitems = 1;
printf("WELCOME TO RESTAURANT MAC C- Take Order\n\n");
printfood;
while (orderitems == 1)
{
fPointer1 = fopen ("Food.txt", "a+");
printf("Enter ITEM CODE (0 to Quit, 100 for Main Menu) : ");
scanf("%d", &item);
if (item == 100) break;
else if (item == 0) //final approach//
{
fPointer2 = fopen ("Invoice.txt", "a+");
de;
fclose (fPointer2);
return 0;
}
else if (item == 900)
{
for (i=0 ; i < 30 ; i++)
printf("%d: %d\n", i, OrderedItems);
}
else if (item > count || item < 1)
{
printf("\n\nITEM CODE not available. Try again\n\n");
}
else
{
OrderedItems[item]++;
printf("%d %d", item, OrderedItems);
}
fclose (fPointer1);
}
}
else printf("Please enter a valid Menu Code\n");
} while (MainMenuCode != 0);
return 0;
}
- printf("%d: %d\n", i, OrderedItems);
+ printf("%d: %d\n", i, OrderedItems[i]);
OrderedItems is an address of the array's first element.
OrderedItems[0] is a value of the array's first element.
OrderedItems[i] is a value of the array's i-th element.
The call to memset should specify how many bytes to set. You need to tell it the size of the type to initialise.
memset(OrderedItems, 0, 30 * sizeof(int))

C while loop infinite loop EOF not working

I am having problem with EOF in my while loop. It does not seem to simply end when EOF is entered but rather does this...
How can I fix it and have the while loop stop and move on. Thanks.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <limits.h>
void findLargestandSmallest(int integer, int* largest, int* smallest, int* average, int count);
int main(void)
{
//Local Declaration
int integer;
int largest;
int smallest;
int average;
int count; // number count
//Starting statemnets
smallest = INT_MAX;
largest = INT_MIN;
count = 0;
// Starting prompts
printf("\nHello this program will take in intagers and print");
printf("\nout the largest, smallest and avarage of integers");
printf("\nenterd int.");
printf("\nPlease enter in a integer ");
while (scanf("%d", &integer) != EOF)
{
if (integer != EOF)
{
count++;
findLargestandSmallest(integer, &largest, &smallest, &average, count);
}
else
{
printf("\n \n");
}
}
printf("\nThe largest number entered is %d and the smallest", largest);
printf("\nwas %d and the average of all the numbers is %d\n", smallest, average);
return 0;
}
void findLargestandSmallest(int integer, int *largest, int *smallest, int *average, int count)
{
int x; // just a holder variable
// finds average
x = 0;
x += integer;
*average = (x / count);
// Finds smallest and largest
if (integer <= *smallest)
{
*smallest = integer;
}
if (integer >= *largest)
{
*largest = integer;
}
printf("Enter another integer or <EOF> to quit ");
return;
}
[1]: http://i.stack.imgur.com/P0307.png
UPDATE: I found out what I was doing wrong. Its simple. In the while loop while(scanf("%d", &integer) != EOF) don't set it like that but like this (scanf("%d", &integer)) EOF is understood. To simply call it in DOS use use "Ctrl+Z" on your last input. i.e "number^Z" is how it will look after using "Ctrl+Z" Also here is the better and working code for this problem for anyone else that runs into this.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <limits.h>
void findLargestandSmallest(int integer, int* largest, int* smallest);
int main(void)
{
//Local Declaration
int integer;
int largest;
int smallest;
int average;
int sum;
int count;
//Starting statemnets
smallest = INT_MAX;
largest = INT_MIN;
count = 0;
sum = 0;
// Starting prompts
printf("\n--------------------------------------------------------");
printf("\n- Hello, this program will take in intagers and print -");
printf("\n- out the largest, smallest, and avarage of the -");
printf("\n- integers enterd. -");
printf("\n- NOTE: To quit: use \"Ctrl+Z\" on the last integer -");
printf("\n- you enter i.e \"number^z\" -");
printf("\n--------------------------------------------------------\n");
printf("\nEnter integers\n");
// Finds largest and smallest number
while (scanf("%d", &integer))
{
sum += integer;
count++;
findLargestandSmallest(integer, &largest, &smallest);
}
// Finds average
count--;
average = (sum / count);
// End prompts
printf("\n--------------------------------------------------------");
printf("\nThe largest number entered was %d, the smallest", largest);
printf("\nwas %d, and the average of all the numbers is %d.", smallest, average);
printf("\n--------------------------------------------------------");
printf("\nGoodbye\n");
return 0;
}
void findLargestandSmallest(int integer, int *largest, int *smallest)
{
if (integer < *smallest)
{
*smallest = integer;
}
if (integer > *largest)
{
*largest = integer;
}
return;
}
scanf returns the number of elements successfully converted. If it can't convert any, it returns 0. EOF is only returned for end-of-file (on Unix a Control-D).
So you should modify your program to save the return value from scanf and then test it for 0 and EOF separately.
It is also pointless to compare the integer variable with EOF, since all you can possibly know about EOF is that it is a negative integer. Read the scanf manual page and understand what it does and what it returns when and where. That'll solve the puzzle. :-)
Alright, some more hints. Can you make sense of this?
for (;;) {
int successfully_converted = scanf("%d", &integer);
if (successfully_converted == EOF) {
/* Do something when the user is tired of typing. */
puts("Thank you for an enjoyable game.\n");
exit(0);
} else if (successfully_converted == 0) {
puts("This didn't look like an integer\n");
exit(1);
} else {
/* Do something with integer. */
}
}
You are not comparing the integer value with EOF
. You are comparing the scanf result with EOF..
Here as you enter 1 value each time, scanf result will be 1.
So evrrytime the while loop condition fails and infinite loop is generated.
Also if you EOF then what character would you enter to end the loop???
So EOF should not be used.
So I would suggest you to use do while loop
do
{
scanf("%d",&integer);
....
...
printf("enter 1 to continue");
scanf("%d",&check);
}while(check == 1);
Test the result of scanf() against EOF, 0, and 1.
int cnt;
while ((cnt = scanf("%d", &integer)) == 1) {
count++;
findLargestandSmallest(integer, &largest, &smallest, &average, count);
}
if (cnt == 0) {
printf("Something other than an integer was entered.\n");
}
else if (cnt == EOF) {
printf("EOF or I/O Error occurred.\n");
}
// Add the following for debug if desired
else {
printf("Should _never get here!\n");
}
...
UPDATE: I found out what I was doing wrong. Its simple. In the while loop while(scanf("%d", &integer) != EOF) don't set it like that but like this (scanf("%d", &integer)) EOF is understood. To simply call it in DOS use use "Ctrl+Z" on your last input. i.e "number^Z" is how it will look after using "Ctrl+Z" Also here is the better and working code for this problem for anyone else that runs into this.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <limits.h>
void findLargestandSmallest(int integer, int* largest, int* smallest);
int main(void)
{
//Local Declaration
int integer;
int largest;
int smallest;
int average;
int sum;
int count;
//Starting statemnets
smallest = INT_MAX;
largest = INT_MIN;
count = 0;
sum = 0;
// Starting prompts
printf("\n--------------------------------------------------------");
printf("\n- Hello, this program will take in intagers and print -");
printf("\n- out the largest, smallest, and avarage of the -");
printf("\n- integers enterd. -");
printf("\n- NOTE: To quit: use \"Ctrl+Z\" on the last integer -");
printf("\n- you enter i.e \"number^z\" -");
printf("\n--------------------------------------------------------\n");
printf("\nEnter integers\n");
// Finds largest and smallest number
while (scanf("%d", &integer))
{
sum += integer;
count++;
findLargestandSmallest(integer, &largest, &smallest);
}
// Finds average
count--;
average = (sum / count);
// End prompts
printf("\n--------------------------------------------------------");
printf("\nThe largest number entered was %d, the smallest", largest);
printf("\nwas %d, and the average of all the numbers is %d.", smallest, average);
printf("\n--------------------------------------------------------");
printf("\nGoodbye\n");
return 0;
}
void findLargestandSmallest(int integer, int *largest, int *smallest)
{
if (integer < *smallest)
{
*smallest = integer;
}
if (integer > *largest)
{
*largest = integer;
}
return;
}

Add values in same line in C programming

I want to print multiple values on same line and then go to next line and print the same values and goes to next line and so on.
for ex:-
3 5 10
2 7 15
in C language.
#include <stdio.h>
int main()
{
int a,b,n,i,j,l;
printf("Enter total digit in a line:");
scanf("%d",&n);
printf("Enter number of lines:");
scanf("%d",&l);
for(i=1;i<=l;i++)
{
for(j=1;j<=n;j++)
{
printf("enter values for line :");
scanf("%d",&n);
}
}
}
For each line, read input as string with e.g. fgets, then use e.g. strtok in a loop to extract each number and use strtol to convert the numbers to integer values, and add them up.
The above solution contains of four parts, so lets split it up and do one at a time.
For each line, read input with fgets.
This is pretty simple, as all you have to do is use your outer loop and ask the user to enter numbers there, as well as read the input there as well:
for (i = 1; i <= l; i++)
{
printf("Enter numbers for line number %d: ", i);
char input[128];
fgets(input, sizeof(input), stdin);
}
Use strtok in a loop to extract each number from the input.
for (i = 1; i <= l; i++)
{
/* The code to read the input... */
char *pointer = strtok(input, " ");
while (pointer != NULL)
{
/* `pointer` is now pointing to the next space-delimited number */
/* Find the next space-delimited number */
pointer = strtok(NULL, " ");
}
}
Use strtol to convert the numbers to integer values.
{
/* `pointer` is now pointing to the next space-delimited number */
/* Convert string to number */
long value = strtol(pointer, NULL, 10);
/* Find the next space-delimited number... */
}
Finally add all values in the line.
for (i = 1; i <= l; i++)
{
long sum = 0;
/* ... */
{
long value = strtol(pointer, NULL, 10);
sum += value;
}
printf("The sum of all values on line %d is %ld\n", i, sum);
}
Putting it all together, we get this code:
for (i = 1; i <= l; i++)
{
printf("Enter numbers for line number %d: ", i);
char input[128];
fgets(input, sizeof(input), stdin);
long sum = 0;
char *pointer = strtok(input, " ");
while (pointer != NULL)
{
/* `pointer` is now pointing to the next space-delimited number */
/* Convert string to number */
long value = strtol(pointer, NULL, 10);
sum += value;
/* Find the next space-delimited number */
pointer = strtok(NULL, " ");
}
printf("The sum of all values on line %d is %ld\n", i, sum);
}
Note: The above code have no error checking. I leave that as an exercise to the readers.
E.g.
#include <stdio.h>
int main(void){
int n, l, i, j;
printf("Enter total digit in a line:");
scanf("%d", &n);
printf("Enter number of lines:");
scanf("%d", &l);
for(i=1;i<=l;i++){
long sum =0;
printf("enter values for line : ");
for(j=1;j<=n;j++){
int num;
scanf("%d", &num);//n : Names are colliding
sum += num;
}
printf("sum of line : %ld\n", sum);
}
return 0;
}

Resources