I'm wondering on how to basically input exactly 5 numbers (integers specifically) in one line using simple commands such as while, if and arrays. For example:
if I input 5 numbers separated by spaces,
1 2 3 4 5
program would print
1 2 3 4 5
but, If I input less than 5 or more than 5,
1 2 3 4
program would print
invalid input.
So far I have
#include<stdio.h>
int main(int argc,char *argv[]){
int array[5], numbers;
numbers = 0;
while (numbers < 5) {
scanf("%d", &array[numbers]);
numbers = numbers + 1
}
printf("%d %d %d %d %d\n", array[0], array[1], array[2], array[3], array[4]);
return 0;
}
What about if we assign all the array cell with 9999 (a number not used by the program). And we make a loop to check that each array has been changed to a new value and if its still 9999 it would be invalid. But problem here still lies, how would we just grab one line of different number of input and move on. E.g input 2 3
Output 2 3 9999 9999 9999
Or input 2 3 4
Output 2 3 4 9999 9999
If you want to force the input to be on one line, first read the input and then parse it:
char line[100];
fgets(line, 100, stdin);
char x[100];
int n = sscanf(line, "%d %d %d %d %d %s", array, array+1, array+2, array+3, array+4, x)
if (n != 5)
printf("invalid input\n");
else
printf("read 5 numbers\n");
The x is added to detect if too much was read.
EDIT
To enter 5 numbers you may use
int a[5];
char x;
scanf("%d %d %d %d %d", &a[0], &a[1], &a[2], &a[3], &a[4]);
while(scanf("%*[^\n]%*c")) {
scanf("%c", &x); // after 5 ints were loaded, get rid of rest of the elements untill new line symbol
}
printf("%d %d %d %d %d\n\n", a[0], a[1], a[2], a[3], a[4]);
It will ignore everything after 5 numbers (in fact, it will write to x untill new line symbol appears), but there is no way to easily set number of numbers to read in this case.
You just have to note it will not work if you have less than 5 ints in line.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int intRead(int array[], int size){
char buff[128];
int status=!0;//status == 0, Something happened!
printf("\ninput %d integer:", size);
while(fgets(buff, sizeof(buff), stdin)){
int count=0;
char*p;
for(p=buff;NULL!=(p=strtok(p, " \t\n"));p=NULL){
char *ck;
int i;
i=(int)strtol(p, &ck, 0);
if(*ck){
fprintf(stderr, "invalid input:can't parse of int <<%s>>\n", p);
status=0;
continue;
}
if(count < size){
array[count++]=i;
continue;
}
count = size + 1;//more than
break;
}
if(count < size)
fprintf(stderr, "invalid input: less than %d\n", size);
else if(count == size) return status;
if(count > size)
fprintf(stderr, "invalid input: more than %d\n", size);
printf("\ninput %d integer:", size);
status = !0;
}
return 0;
}
int main(int argc,char *argv[]){
int array[5];
intRead(array, 5);//or: while(!intRead(array, 5));
printf("%d %d %d %d %d\n", array[0], array[1], array[2], array[3], array[4]);
return 0;
}
Related
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int n, k;
scanf("%d %d", &n, &k);
printf("%d %d\n", n, k);
return 0;
}
In the above sample code passing to the input
1 2
Would produce an expected output of
1 2
However passing any char in-between these two digits such as
1. 2 or 1 d 3
Will result in a strange output of the following
1 32766
I would like to know why this occurs, as it was to my belief that scanf would skip over any non-digit input.
You should check the return value of scanf, which tells you the number of data that are read into the passed arguments, here it is k.
In your case, the return value will be zero as %d cannot be used to read in a char in C. If the first input is a char it will be 0, 1 if the first value is int and the second value is a char, 2 if both of the values are int.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int n, k, rc;
rc = scanf("%d %d", &n, &k);
if (rc != 2)
{
printf("scanf failed to set the values for n and k\n");
}
else
{
printf("valid input for n and k\n");
}
printf("%d %d\n", n, k);
return 0;
}
So the int k is uninitialized and thus it will store some random value as scanf failed to set the value for this variable.
(Too long for a comment.) The following answers OP's followup question from a comment.
is there anyway to pass over the character that stops the input stream?
scanf may not be the best or easiest way to do that, but it's still possible to do it using the %n format specifier which returns the offset into the original string during parsing. Following is an example, which uses sscanf but can be easily adapted to scanf.
#include <stdio.h>
void read_three_ints(const char *str) {
int a, n1, b, n2, c;
int ret = sscanf(str, "%d %n%d %n%d", &a, &n1, &b, &n2, &c);
switch(ret)
{
case 3:
printf("'%s': a = %d, b = %d, c = %d\n", str, a, b, c);
break;
case 2:
printf("'%s': a = %d, b = %d, error parsing c = `%s`\n", str, a, b, str + n2);
break;
case 1:
printf("'%s': a = %d, error parsing b = `%s`\n", str, a, str + n1);
break;
case 0:
printf("'%s': error parsing a\n", str);
break;
default:
printf("'%s': scanf error %d\n", str, ret);
break;
}
}
int main()
{
read_three_ints("1 2 3");
read_three_ints("1 2 x");
read_three_ints("1, 2 ");
read_three_ints(";1 ");
read_three_ints("");
return 0;
}
Output:
'1 2 3': a = 1, b = 2, c = 3
'1 2 x': a = 1, b = 2, error parsing c = `x`
'1, 2 ': a = 1, error parsing b = `, 2 `
';1 ': error parsing a
'': scanf error -1
I have started an intro to programming course that uses the C language and we have an assignment to make a program that takes a 5 digit number from the user such as 12345 and it prints it out as 1 2 3 4 5.
I tried to google around for help but all the answers given used code way too complicated for my understanding considering the course just started and we have only learned printf and scanf, if and switch statements and while and for loops.
I tried putting all the numbers given into separate int variables which made the program stop and then tried to put them into chars but the testing program said it was wrong since we are supposed to use int.
Is there a simple way to do this?
EDIT:
What I have tried:
#include <stdio.h>
int main(void) {
int num1,
num2,
num3,
num4,
num5;
printf("Give 5 digit number > ");
scanf("%d%d%d%d%d", &num1, &num2, &num3, &num4, &num5);
printf("Seperated number is %d %d %d %d %d", num1, num2, num3, num4, num5);
return (0);
}
Also tried that code but with char variable type but that wasn't allowed it has to be int.
The testing program gives an expected output which for 00001 is Given number 1 seperated is 0 0 0 0 1 or for -12321 is Given number -12321 seperated is -1 -2 -3 -2 -1
You can read user input into a char array using scanf.
Then using for loop you can loop through each char (1,2,3,4,5)
Use printf in the loop to print from array and a space. printf("%c ", input_from_user[i]);
I suppose the point here is understanding how C stores things. You're reading in characters (that is a set of 8-byte values that are commonly displayed as letters) and converting each one to an integer (commonly a 32-bit value that stores a number)
So you want to read the input using scanf to get a character string of your 5 digits, then use a for loop to go over each one, reading a single character from the string and converting it into an integer variable. then printf that variable plus a space, and move on to repeat the next digit.
If you read the digits in one by one then its even easier as you only need the for loop to read, convert, and print each digit as its read.
According to the question, you would be given a number of 5 digits and then you have to print the digits of that number as well.
So, in your program, you have taken five digits individually as input, to me it seems wrong. I am giving my solution below:
#include <stdio.h>
int main(void)
{
int num,idx=0,i=0;
int arr[5]; // for storing the five digits;
scanf("%d",&num);
while(num){ //loop will terminate when the num = 0
int digit = num%10;
num = num/10;
arr[idx] = digit;
idx++;
}
//Printing from the last, as the digit are stored in reverse order.
for(int i=4;i>=0;i--){
printf("%d ",arr[i]);
}
return 0;
}
I don't really understand the purpose of this exercise. If you are learning C, the last function you should learn is scanf. It boggles the mind that so many people teach it early. Try very hard not to use scanf at all until you understand the language well enough to realize that you probably never need it. That said, I suppose the point of the exercise is to do something like:
#include <stdio.h>
int
main(void)
{
unsigned num;
char buf[6];
printf("Give 5 digit number > ");
if( scanf("%u", &num) != 1 || num > 99999 ) {
fprintf(stderr, "Invalid input\n");
return 1;
}
printf("Separated number is");
snprintf(buf, sizeof buf, "%d", num);
for( int i = 0; buf[i]; i++ ) {
printf(" %c", buf[i]);
}
putchar('\n');
return 0;
}
Although likely beyond OP's stage, code can use recursion to print the value from most significant to least.
#include <stdio.h>
void print_digit(int n, int depth) {
if (depth > 1) print_digit(n/10, depth - 1); // print "left" digits first
printf(" %d", n%10);
}
void print_5(int n) {
// To do: checking for correct spelling
printf("Given number %d seperated", n); // or separated
print_digit(n, 5);
printf("\n");
}
int main(void)
{
print_5(1);
print_5(-12321);
return 0;
}
Output
Given number 1 seperated 0 0 0 0 1
Given number -12321 seperated -1 -2 -3 -2 -1
You could use your approach by limiting the number of bytes you read for each number:
#include <stdio.h>
int main(void) {
int num1, num2, num3, num4, num5;
printf("Give 5 digit number > ");
if (scanf("%1d%1d%1d%1d%1d", &num1, &num2,&num3, &num4, &num5) == 5) {
printf("Separated number is %d %d %d %d %d\n", num1, num2, num3, num4, num5);
}
return 0;
}
Note however that this is probably not what you are expected to do. You should instead read a single number and decompose it into its five digits:
#include <stdio.h>
int main(void) {
int num;
printf("Give 5 digit number > ");
if (scanf("%d", &num) == 1 && num >= 10000 && num <= 99999) {
printf("Separated number is %d %d %d %d %d\n",
num / 10000, num / 1000 % 10, num / 100 % 10, num / 10 % 10, num % 10);
}
return 0;
}
I have a text file where info looks like this:
he he 5 5 5 5 5 5 5 5 5 5 5
ne ne 5 5 5 5 5 5 5 5 5 5 5
and I can keep adding to it.
What I want to do is have a function that reads that info and puts in into struct array, but I'm having trouble with pointers and figuring out how to read one line, then next one etc. I'm aware function that reads atm is broken, I tried bunch of stuff and could not figure it out. Sorry if its a bit silly question.
Source.c
int main(void) {
int opcija=0;
U *ucenici=NULL;
while(opcija !=4){
printf("Odaberite opciju:\n 1.Stvaranje datoteke ucenici.txt\n 2.Upisivanje novog ucenika\n 3.Pregled ucenika\n 4.Izlaz iz programa\n");
scanf("%d", &opcija);
switch (opcija) {
case 1:
kreiranje();
break;
case 2:
dodavanje();
break;
case 3:
void citanje1(U *ucenici);
int j=brojanjelinija();
for (int i = 0;i < j; i++) {
printf("%d. %s %s\n", i + 1, ucenici[i].ime, ucenici[i].prezime);
}
}
}
return 0;
}
void citanje1(U **ucen) {
FILE* pok = NULL;
pok = fopen("ucenici.txt", "r");
int broj = brojanjelinija();
ucen = (U**)malloc(broj * sizeof(U*));
for (int i = 0;i < broj;i++) {
fscanf(pok, "%s %s %d %d %d %d %d %d %d %d %d %d %d \n", ucen[i]->ime, ucen[i]->prezime, ucen[i]->dan, ucen[i]->mjesec, ucen[i]->godina, ucen[i]->mat, ucen[i]->hrv, ucen[i]->eng, ucen[i]->hidr, ucen[i]->fiz, ucen[i]->term, ucen[i]->oet, ucen[i]->tehmat);
}
free(ucen);
}
Header.h
#ifndef HEADER_H
#define HEADER_H
typedef struct ucenik{
char ime[10];
char prezime[10];
int* dan;
int* mjesec;
int* godina;
int* mat;
int* hrv;
int* eng;
int* hidr;
int* fiz;
int* term;
int* oet;
int* tehmat;
}U;
void kreiranje(void);
void dodavanje();
int brojanjelinija();
void citanje1(U);
#endif // HEADER_H
All your ucinek data members that you want to use to store integers should actually store int instead of int*:
int dan;
int mjesec;
...etc...
Remember that a pointer tells the compiler where to find some other data, rather than storing a meaningful value itself.
Your fscanf line should then change to pass pointers to the integers, which you can get using the & operator:
fscanf(pok, "%s %s %d %d %d %d %d %d %d %d %d %d %d\n",
ucen[i]->ime, ucen[i]->prezime, &ucen[i]->dan, &ucen[i]->mjesec,
&ucen[i]->godina, &ucen[i]->mat, &ucen[i]->hrv, &ucen[i]->eng,
&ucen[i]->hidr, &ucen[i]->fiz, &ucen[i]->term, &ucen[i]->oet,
&ucen[i]->tehmat);
Those pointers tell fscanf where to store the parsed values.
I never use fscanf without checking the returned value either: it says how many values were successfully parsed from the input and stored. If it's not the number you're expecting, you should print an error message.
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);
My console keeps on crashing after entering a few numbers. I am trying to get an array of 10 numbers from the user thru the console and then taking count of positives, negatives, evens, and odds. What am I doing wrong?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int pos, neg, even, odd;
int nums[10];
printf("Give me 10 numbers: ");
pos = neg = even = odd = 0;
for(int i = 0; i < 10; i++){
scanf(" %d", nums[i]);
if(nums[i] > 0){
pos++;
if(nums[i] % 2 == 0){
even++;
}
else{
odd++;
}
}
else{
neg++;
}
}
printf("Positives: %d, Negatives: %d, Evens: %d, Odds: %d\n", pos, neg, even, odd);
return 0;
}
In your code,
scanf(" %d", nums[i]);
should be
scanf(" %d", &(nums[i]));
or,
scanf(" %d", nums+i);
as you need to pass the pointer to variable as the format specifier's argument in scanf() .
To elaborate, %d expects a pointer to int and what you're supplying is an int variable. it invokes undefined behavior.
That said,
Always check the return value of scanf() to ensure proper scanning.
int main() should be int main(void) to conform to the standard.
Modify scanf like scanf(" %d", &nums[i]);
scanf(" %d", nums[i]);
Scanf expects a pointer to a location to write to, and you're not giving it one.
Change your scanf to:
scanf(" %d", &(nums[i]));
to make your program work.
With this change I tested your program with stdin of
20 10 9 1 39 1 2 2 31 1
And recieved output:
Give me 10 numbers: Positives: 10, Negatives: 0, Evens: 4, Odds: 6
ideone of the thing for your testing purposes.
Change scanf(" %d", nums[i]); to scanf(" %d", &nums[i]);, because scanf() needs addresses. The parentheses around nums[i] isn't necessary, and may effect readability.
Also note that 0 is even, but not negative.
When scanf is usedto convert numbers, it expects a pointer to the corresponding type as argument, in your case int *:
scanf(" %d", &nums[i]);
This should get rid of your crash. scanf has a return value, namely the number of conversions made or the special value EOF to indicate the end of input. Please check it, otherwise you can't be sure that you have read a valid number.
When you look at your code, you'll notice that you don't need an array. Afterreading the number, you don't do aything with the array. You just keep a tally of odd, even and so on numbers. That means you just need a single integer to store the current number. That also extends your program nicely to inputs of any length.
Here's a variant that reads numbers until the end of input is reached (by pressing Ctrl-D or Ctrl-Z) or until a non-number is entered, e.g. "stop":
#include <stdlib.h>
#include <stdio.h>
int main()
{
int count = 0;
int pos = 0;
int neg = 0;
int even = 0;
int odd = 0;
int num;
while (scanf("%d", &num) == 1) {
count++;
if (num > 0) pos++;
if (num < 0) neg++;
if (num % 2 == 0) even++;
if (num % 2 != 0) odd++;
}
printf("%d numbers, of which:\n", count);
printf(" %d positive\n", pos);
printf(" %d negative\n", neg);
printf(" %d even\n", even);
printf(" %d odd\n", odd);
return 0;
}
Change scanf statement after for loop to
scanf(" %d", &nums[i]);