Question

In: Computer Science

New to C programming and I am stuck. The program will print "no input" if no...

New to C programming and I am stuck. The program will print "no input" if no input is given. If a command line argument is given the program will print "input:" followed by the user input. Below is the code I put together to do as desired but I get errors. What do I need to fix to get my code to compile correctly?

#include <stdio.h>
#include <stdlib.h>

int main ()

{
char input;
scanf("%c", &input);
if (input == NULL)
{
printf("no input");

}
else
{
printf("input:",input);
  
}
return 0;
}

Solutions

Expert Solution

The mistakes i am able to find are :-

a) In the if condition , ( if ( input == NULL ) ) , you are trying to check if the user has not enterned any character , so if the user has not enterned the character the character input will be a null character and can be compared by the checking if input == '\0'  . Here backslash zero or escaped zero ( \0 ) is assigned to a character which is null. So your if condition will become ( if( input == '\0' ) ).

b) Now in the else condition , ( else { printf("input:",input)} ) , whenever you want to print any variable using the printf function in C langauge , you need to use the format specifier for that variable in the printf statement. For example the format specifier for int is %d , for char it is %c. So the newly modified else statement will become  ( else { printf("input: %c",input)} ) . I have used %c format specifier here because input is of type char.

So the modified code will become :-

#include <stdio.h>
#include <stdlib.h>

int main ()

{
char input;
scanf("%c", &input);
if (input == '\0') //changed this line
{
printf("no input");

}
else
{
printf("input: %c",input); //changed this line
  
}
return 0;
}


Related Solutions

Hello, I stuck doing c++ program The program will be recieved from the user as input...
Hello, I stuck doing c++ program The program will be recieved from the user as input name of an input file and and output.file. then read in a list of name, id# and score from input file (inputFile.txt) and initilized the array of struct. find the student with the higher score and output the students info in the output file obtain the sum of all score and output the resurl in output file. prompt the user for a name to...
This program needs to be in C++. I have barely gotten anywhere but am already stuck....
This program needs to be in C++. I have barely gotten anywhere but am already stuck. Any help with this one would be greatly appreciated. Write a program that simulates playing a simplified version of the game "Candy Land." In "Candy Land" players take turns drawing cards that have a colored square on them. The player then travels on the board according to the color on the card. The first person to reach the end of the board wins. In...
Hi, (C programming) I am looking to write a program that will encrypt a text file...
Hi, (C programming) I am looking to write a program that will encrypt a text file automatically once program has opened, and have the option to decrypt it in a menu in my program. I have multiple text files that I need to encrypt, and would like if the program could encrypt all of them at once. I would also only like to decrypt the a text file once the name has been entered into a scanf function.
I have been able to determine a. but am stuck on b. and c. for this...
I have been able to determine a. but am stuck on b. and c. for this question... If Wild Widgets, Inc., were an all-equity company, it would have a beta of 0.9. The company has a target debt–equity ratio of .4. The expected return on the market portfolio is 12 percent, and Treasury bills currently yield 4.1 percent. The company has one bond issue outstanding that matures in 20 years and has a coupon rate of 7.2 percent. The bond...
I am stuck on this problem and I am not sure what the solution is. In...
I am stuck on this problem and I am not sure what the solution is. In C Write item.h and item.c. In item.h, typedef a struct (of type t_item) which contains the following information: t_item: char name[MAX_ITEM_NAME_STRING]; char description[MAX_ITEM_DESCRIPTION_STRING]; Make sure that MAX_ITEM_NAME_STRING and MAX_ITEM_DESCRIPTION_STRING are defined with suitable sizes in your item.h. Typical values are, 25 and 80, respectively. Add the following interface definition to item.h: int item_load_items(t_item items[], int max_items, char *filename); Returns the number of objects loaded...
C Programming Run Length Encoder (Compression). I am writing a program that takes an image (from...
C Programming Run Length Encoder (Compression). I am writing a program that takes an image (from a text file) and looks for patterns of 2 or more duplicate values. The program should replace these values with the following pattern:  2 such characters followed by an Integer ( which represents number of occurrences of each character), followed by an asterisk. For example say the input of the non-compressed image was: ,,,,)H 7. i.e. it consists of four commas, one closed bracket, the...
WITHOUT USING POINTERS. This is C programming. I am writing a program which should calculate standard...
WITHOUT USING POINTERS. This is C programming. I am writing a program which should calculate standard deviation of an array of exam scores and then add the standard deviation to each array element. It should print the original array, the standard deviation, and the new rounded adjusted array. I included my question as a comment in the line of code that is giving me an issue. (Removing the a from E-x-a-m as Chegg doesn't allow the word.) #include <stdio.h> #include...
Hello this is for C++ language. I am currently stuck on creating my api for Day...
Hello this is for C++ language. I am currently stuck on creating my api for Day Trading Stocks. as follows I need an api for *//Function Signature * * parameter: * * Return Value: ** *// Write the following function taking in an integer vector (vector &prices) consisting of all prices, in chronological order, for an hypothetical instrument. Your function recommends the maximum profit an investor can make by placing AT MOST one buy and one sell order in the...
In C programming, I am trying to search for the names of people that in this...
In C programming, I am trying to search for the names of people that in this DOISigned.txt file, however I am having trouble getting the first and last names of the multiple people named john, my current code only searches for John once and then it terminates,here is my current code #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> #include <stdlib.h> #define BUF_SIZE 0x3000 char buf[BUF_SIZE]; int main() {    char* inputFile = "DOISigners.txt";    FILE* fp;    fp = fopen(inputFile, "r");...
Hi! I am in an intro level Finance course and I am stuck on this problem....
Hi! I am in an intro level Finance course and I am stuck on this problem. Any help would be greatly appreciated. I am deciding on opening a restaurant. I was able to scrape together some capital from friends and family, but I must pay them back in 4 years at 12% per annum. I figure that it will cost me $165,000 to start up with rent, deposits, equipment, salaries, chicken, basil, rice, etc. for the first year, but I...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT