C Program Help: Input Name & Print A/B or Error

  • Thread starter samarth0157
  • Start date
  • Tags
    Program
In summary: Have you written some code?In summary, the conversation is about a request for a program in C that inputs a name and prints the name if the first character is 'a' or 'b' and prints an error message otherwise. The person asking for help is advised to post their question in the appropriate homework help sub-section and is given links to resources on C programming and the use of pointers. Pseudo code is also provided as a starting point for the program, but the person is reminded to show effort in their own coding before seeking further assistance.
  • #1
samarth0157
1
0
GIVE A program of c to input a name and print the name if the first char of name is a or b else print error message.
 
Physics news on Phys.org
  • #2
Welcome to PhysicsForums!

We don't do homework (or things that aren't homework that nevertheless look like homework) for people here. Also, homework should go in the appropriate homework help sub-section (I've contacted a moderator to get this post moved where other people can help you help yourself, as Jerry Maguire says):
https://www.physicsforums.com/forumdisplay.php?f=152

To get you started, do you know C? Specifically, how to make use of scanf():
http://en.wikibooks.org/wiki/C_Programming/Simple_input_and_output#Input_using_scanf.28.29

...and how to structure an if statement?
http://en.wikibooks.org/wiki/C_Programming/Control#The_If-Else_statement
 
  • #3
You will need to know about strings and pointers. Have you gone over those yet?

pseudo code
declarations
get input from user in a string(char array)
use pointer to check first letter of name
if (first letter is 'a' or 'b') print name
else don't print name

Does this help you? If not let me know I will try and explain better.

EDIT: Actually you do not need to know about pointers I suppose you can just compare the first element of the string

char name[50];
get input
if(name[0]=='a'||name[0]=='b')
 
Last edited:
  • #4
samarth0157 said:
GIVE A program of c to input a name and print the name if the first char of name is a or b else print error message.

Your thread has been moved to the Homework Help section of the PF.

You need to show some effort on your part before we can be of any more help.
 
  • #5


Sure, here is a sample program in C that can achieve the desired output:

#include <stdio.h>
#include <string.h>

int main()
{
//declare a char array to store the name
char name[20];

//prompt the user to input a name
printf("Enter a name: ");

//use scanf to read the input and store it in the name array
scanf("%s", name);

//check if the first character of the name is either 'a' or 'b'
if(name[0] == 'a' || name[0] == 'b')
{
//if yes, print the name
printf("Name: %s\n", name);
}
else
{
//if not, print an error message
printf("Error: First character of name must be 'a' or 'b'\n");
}

return 0;
}

Sample Output 1:
Enter a name: Alice
Name: Alice

Sample Output 2:
Enter a name: Bob
Name: Bob

Sample Output 3:
Enter a name: Carol
Error: First character of name must be 'a' or 'b'
 

1. How do I input a name in a C program?

To input a name in a C program, you can use the scanf() function. This function takes in user input and stores it in a variable. For example, if you want to input a name and store it in a variable called name, you can use the following code:

scanf("%s", name);

2. How do I check if the input name starts with the letter A or B?

To check if the input name starts with the letter A or B, you can use an if statement. First, you need to access the first letter of the input name using array indexing. Then, you can compare it with the letters A and B using the == operator. For example:

if(name[0] == 'A' || name[0] == 'B') {
    // input name starts with A or B
}

3. What if the input name does not start with the letter A or B?

If the input name does not start with the letter A or B, you can print an error message using the printf() function. For example:

printf("Error: Input name does not start with A or B");

4. How do I handle multiple inputs in a C program?

To handle multiple inputs in a C program, you can use a while loop. This loop will continue to run until the user inputs a specific value, such as -1. Inside the loop, you can use the scanf() function to take in multiple inputs and perform the necessary operations. For example:

while(input != -1) {
    scanf("%d", &input);
    // perform operations on input
}

5. How do I end the C program?

To end a C program, you can use the return statement. This statement will exit the program and return control to the operating system. For example, if you want to end the program after printing the input name, you can use the following code:

printf("%s", name);
return 0;

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
932
  • Engineering and Comp Sci Homework Help
Replies
22
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
23
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
Back
Top