Why is my scanf for my array running before the for loop? Output [enter image description here][1]
#include <stdio.h>
void main()
{
int num,i,arr[10];
printf("The number you want to add with each number is:");
scanf("%d \n",&num);
// printf("%d \n",num);
printf("the 10 numbers are:\n");
for(i=0;i<=9;i++){
scanf("%d \n",&arr[i]);
}
printf("The new array is:");
for(i=0;i<=9;i++){
arr[i] = arr[i]+ num;
printf("%d \n",arr[i]);
}
}
It's because you are using trailing white spaces in your scanf() syntax. In addition, you are using \n in scanf and your scanf here is not only scanning the number %d, but scanning the whole thing in between quotations (" "). and that causes unexpected errors. So you should write your code in another way.
Related
I'm trying to make a simple voucher. So, I used a multidimensional string. But facing trouble including space in those strings. Instead, I took words as input. But is there any way to include space? My code is given below-
#include<stdio.h>
#include<string.h>
int main(){
int sum =0, n, i;
puts("Please input how many transactions you want to enlist: ");
scanf("%d", &n);
char list[301][51];
int amount[301];
puts("Please enter the name of your transaction and the amount: (Press space or enter to toggle between name and amount . And avoid using spaces in the name; use underscore instead.)");
for(i=0; i<n; i++){
scanf("%s %d", &list[i], &amount[i]);
sum += amount[i];
}
list[0][n+1] = '\0';
amount[n+1] = '\0';
puts("");
printf("\t\t\t\t Voucher\n\n");
puts(" Ser.|\t Name \t\t\t\t\t\t\t|Amount");
puts("------------------------------------------------------------------------------------------------------------");
for(i=0; i<n; i++ ){
printf(" %03d |\t %-50s\t|%6d\n", i+1, list[i], amount[i]);
}
puts("------------------------------------------------------------------------------------------------------------");
printf(" | Total\t\t\t\t\t\t\t|%6d", sum);
puts("");
return 0;
}
For this you could use the %[ scanf specifier, to read all characters until you hit a digit, and write it into list[i].
This will leave you with a trailing space in list[i], but that can be trimmed if you don't want it.
The scanf call could then look something like
scanf(" %50[^0-9]%d", list[i], &amount[i]);
Note the leading space in the format string, to tell scanf to skip white-space (like the newline from the previous line), and the width-specifier to not read more than can fit in line[i].
Of course, that prevents you from having numbers inside the string you read. To solve this problem you need to go a more complicated route.
For example by reading the whole line into a buffer, and then find the last space in the string. You can then copy the contents before the last space to list[i], and convert the contents after to an int value for amount[i].
I'm new to C programming. I was trying to write a program that accepts an integer from user and displays its multiplication table up to 10 multiples.
This is my program:
#include <stdio.h>
int main ()
{
int number;
int count = 1;
int sum;
printf("Enter a number to display its table: ");
scanf(" %i ", &number);
while (count <=10)
{
sum = number * count;
printf("%i x %i = %i\n", number, count, sum);
count += 1;
}
return 0;
}
Compilation successfully completes, but when I execute the output file, nothing happens, the terminal is stuck at nothing, i've to press ctrl+c to get out..
This is due to the spaces used in your scanf command.
If you replace that with
scanf("%i", &number);
you get an instant response.
With your scanf format " %i ", the scanf function will read (and skip) possible leading spaces because of your leading space in the format.
Then it will read the integer.
Then, due to the trailing space, it will read and discard space until it find a non-space input.
Since there's no non-space input afterward, then scanf will block until you give some non-space input.
Solve simply by not having any spaces in the format. Or by entering some extra dummy input (followed by Enter).
The problem resides with the scanf.
Just replace
scanf(" %i ", &number);
with:
scanf("%i", &number);
and it will work.
#include <stdio.h>
int main (){
int number;
int count = 1;
int sum;
printf("Enter a number to display its table: ");
scanf("%d", &number);
while (count <=10){
sum = number * count;
printf("%d * %d = %d\n", number, count, sum);
count += 1;
}
return 0;
}
Note: You can use both %d or %i where %d specifies signed decimal integer while %i specifies integer.
Problem: The problem of your code was using a whitespace before %i.
Wrong:
scanf(" %i ", &number); //Wrong
Right:
scanf("%i", &number); //Right.
I have an attempt, but this site will not let me indent it properly and I keep getting errors
Write the prototype for a function called script that has three input parameters.
The first parameter will be the number of spaces to display at the beginning of a
line. The second parameter will be the character to display after the spaces, and
the third parameter will be the number of times to display the second parameter
on the same line.
script();
int main() {
script();
return 0;
}
double script() {
int spaces, timesCharacter;
char character;
printf("Please enter in order the number of spaces preceeding a character\n");
printf("the specific character to input and \n");
printf("the number of times your want the character to display\n");
printf("in order with a space in between each one: \n");
scanf("%d %c %d", &spaces, &character, ×Character);
printf("%d %c %d", spaces, character, timesCharacter);
}
this line above is the problem line, I cannot figure out how to print spaces preceeding
Write the prototype for a function called script that has three input
parameters.
how to print spaces preceding?
Function with parameter and Prototype, Have look at follow code:
#include <stdio.h>
double script(int spaces,char character, int timesCharacter ); //Function Prototype
int main() {
int spaces, timesCharacter;
char character;
script(spaces, character,timesCharacter); //Function Call
return 0;
}
double script(int spaces,char character, int timesCharacter) { //Function Defination
printf("Please enter in order the number of spaces preceeding a character\n");
printf("the specific character to input and \n");
printf("the number of times your want the character to display\n");
printf("in order with a space in between each one: \n");
scanf("%d %c %d", &spaces, &character, ×Character);
//printf("%d %c %d", spaces, character, timesCharacter);
/*How to add spaces preceding*/
printf("%*c", spaces, character);
//To print the number of times
int i=0;
for(i = 1;i < timesCharacter; i++ ){
printf("%c",character);
}
}
this will not have any error.
So here is my program. It is suposed to write out square of some intiger.
#include <stdio.h>
int main (){
int a;
printf("Type an intiger.");
scanf("%i", &a);
printf("Square of that intiger is %i", a*a);
return 0;
}
When i run a program in Eclipse it first requires me to input a number.I put in 5. And then as output it gives me
Type an intiger.Square of that intiger is 25.
It should first print "Type an intiger" and then the rest. But it just combines two printf commands. What is the problem?
You need a newline character - printf("Type an intiger.\n");
In computing, a newline, also known as a line break or end-of-line
(EOL) marker, or simply break, is a special character or sequence of
characters signifying the end of a line of text.
Also format specifier for integer is %d
scanf("%d", &a);
printf("Square of that intiger is %d", a*a);
If you want it on separate lines you can always add '\n' to the string to get a new line.
#include <stdio.h>
int main (){
int a;
printf("Type an intiger.\n");
scanf("%i", &a);
printf("Square of that intiger is %i", a*a);
return 0;
}
There is 2 problem in it. First, if you input the integer, it should be %d. Example :
scanf("%d", &a);
The second, after the input, you should print \n. So, it will be like this printf("\n");. Take a look at my code :
#include <stdio.h>
int main (){
int a;
printf("Type an intiger.");
scanf("%d", &a);
printf("\nSquare of that intiger is %d", a*a);
return 0;
}
In code::blocks it compiles fine anyway put a \n at the end of the first printf and change %i with %d
Basically I have a C program where the user inputs a number (eg. 4). What that is defining is the number of integers that will go into an array (maximum of 10). However I want the user to be able to input them as "1 5 2 6" (for example). I.e. as a white space delimited list.
So far:
#include<stdio.h>;
int main()
{
int no, *noArray[10];
printf("Enter no. of variables for array");
scanf("%d", &no);
printf("Enter the %d values of the array", no);
//this is where I want the scanf to be generated automatically. eg:
scanf("%d %d %d %d", noArray[0], noArray[1], noArray[2], noArray[3]);
return 0;
}
Not sure how I might do this?
Thanks
scanf automatically consumes any whitespace that comes before the format specifier/percentage sign (except in the case of %c, which consumes one character at a time, including whitespace). This means that a line like:
scanf("%d", &no);
actually reads and ignores all the whitespace before the integer you want to read. So you can easily read an arbitrary number of integers separated by whitespace using a for loop:
for(int i = 0; i < no; i++) {
scanf("%d", &noArray[i]);
}
Note that noArray should be an array of ints and you need to pass the address of each element to scanf, as mentioned above. Also you shouldn't have a semicolon after your #include statement. The compiler should give you a warning if not an error for that.
#include <stdio.h>
int main(int argc,char *argv[])
{
int no,noArray[10];
int i = 0;
scanf("%d",&no);
while(no > 10)
{
printf("The no must be smaller than 10,please input again\n");
scanf("%d",&no);
}
for(i = 0;i < no;i++)
{
scanf("%d",&noArray[i]);
}
return 0;
}
You can try it like this.