May I need help in sorting this out? Basically, I'm tasked to play around with ctype header files. I get input from the user and then I give the output depending on the input provided.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void)
{
char savedPassword[] = "123456";
char username[100], password[100], charInput, decision;
printf("Username: ");
fgets(username, sizeof(username)/sizeof(username[0]), stdin);
printf("Password: ");
scanf(" %s", &password);
if (strcmp(savedPassword, password) == 0)
{
printf("Welcome, %s\n", username);
do
{
printf("Enter any character: ");
scanf(" %c", &charInput);
if(isalpha(charInput))
{
if(isupper(charInput))
{
printf("%c is a capital letter.\n", charInput);
} else {
printf("%c is a small letter.\n", charInput);
}
} else if (isdigit(charInput))
{
printf("%c is a digit.\n", charInput);
} else if (ispunct(charInput))
{
printf("%c is a punctuation.\n", charInput);
} else if (isspace(charInput))
{
printf("%c is a space.\n", charInput);
}
printf("Do you want to exit (Y/N)? ");
scanf(" %c", &decision);
if (toupper(decision) != 'Y' && toupper(decision) != 'N')
{
printf("Please re-enter the correct response.\n");
printf("Do you want to exit (Y/N)? ");
scanf(" %c", &decision);
}
}while(toupper(decision) != 'Y');
printf("Thank you for using the app!");
} else {
printf("Wrong password! Try again!");
}
return 0;
}
I couldn't make isspace() work because it seems like scanf() doesn't detect the whitespace anymore as I have used one before the placeholder.
Thank you!
Related
I'm creating a program that asks for input and then based on the input rejects it or accepts it. This is the code:
while(1){
printf("Enter name: ");
scanf("%s", &name);
if(name[0] == '\0'){
printf("Input Cannot be empty\nExample: bobshmurda\n");
} else {
break;
}
}
printf("Enter age: ");
scanf("%d", &age);
while(!age>= 15){
printf("Age\n");
}
while(1){
printf("Enter MMN: ");
scanf("%d", &mmn);
if (!cvv >= 3){
printf("\nInvalid MMN... Try again\n");
} else {
break;
}
}
while(1){
printf("DOB: ");
scanf("%d", &dob);
if (!exp == 4){
printf("Invalid DOB detected... Format: 0123\n");
} else {
break;
}
}
What i basically want to do is i want to for example IF age is greater than 100 or less than 0 do this, etc. Same with strings how would i do that?
A do-while loop is probably the simplest method:
int is_valid = 0;
do {
// Get user input here
is_valid = validate_input(...);
} while (! is_valid);
// Continue with processing...
How can I end the program when a user enters "quit"? I tried an if statement before the loop and within the loop. I'm pretty new to programming and it's bothering me that I can't figure this out. I even tried a do while loop and that didn't work at all.
int main()
{
char word[30];
char yn;
int loopcount;
yn=0;
loopcount=0;
printf("Enter a word:");
scanf(" %s", word);
printf("Would you like to change the word? y=yes, n=no \n");
scanf(" %c", &yn);
if (yn=='n')
{
return 0;
}
while(yn>0)
{
printf("Enter a new word: \n");
scanf(" %s", word);
printf("New word is: %s \n");
loopcount= loopcount+1;
printf("You have changed the word %d times.\n", loopcount);
printf("Would you like to change the word? y=yes, n=no\n");
scanf(" %c", &yn);
if (yn=='n')
{
return 0;
}
return 0;
}
return 0;
}
Use strcmp()
scanf(" %s", word);
if (strcmp(word, "quit") == 0) {
return 0;
}
I don't know where is my mistake, please I need your help. When I enter a wrong data in my username and password and it will ask if YES or NO to try again. If I type Y as a YES I can't type in username anymore.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main() {
printf ("\n\t\t Welcome To Your Payroll Account!");
printf ("\n\t\t Please Enter Your Username & Password To Proceed.");
printf ("\n\t ********************************************************* \n \n \n");
char password [10], username [10], ch;
char name [10] = "username";
char pass[10] = "password";
char month, ename, address;
float regpay,regov,RD,RDOT,meal;
float SSS,PH,PI,Union,other,SSSsal,Loan,Hloan,ULoan,ELoan,TD;
float TE,Netpay;
int n,day,year,i;
char input;
char again = 'Y';
while (again == 'Y' || again =='y') {
printf("Username: ");
gets (username);
printf("Password: ");
for (i = 0; i<8; i++){
ch = getch();
password[i] = ch;
printf ("*");
}
password[i] = 0;
printf("\n Your password is: ");
for (i =0; i<8; i++) {
printf("%c", password[i]);
}
if(strcmp(pass,password) == 0 && strcmp(name,username) == 0) {
printf("\n \n You are logged in! \n");
printf("Enter # to proceed \n");
scanf("%c", &input);
} else {
printf("\n \n Incorrect username or password");
printf("\n\n Do you want to try again? YES(Y) / NO(N) \n");
printf("Continue? ");
scanf("%c", &again);
again;
if (again == 'N' || again == 'n') {
system ("pause");
return 0;
} else {
again;
}
}
}
system ("pause");
return 0;
}
printf("Continue? ");
scanf("%c", &again);
again;
if (again == 'N' || again == 'n') {
system ("pause");
return 0;
} else {
again;
}
Here while taking char input you are using scanf()
so will give character input plus enter.
again will get the input character, but enter will remains in buffer.
When while loop iterates, gets which is waiting for username input will get enter character automatically from the buffer, so the username input will be skipped.
solution is to add getch() after
scanf("%c", &again);
Hope this will hep you.
I am a beginner in C...
I have made a made a calculator type program which uses four basic functions in C using if-else loop.
I want when the program comes to end(after the user has added, subtracted etc. etc. then there is a option "Y/N" so that the program can be restarted???"
Here is the sample of the code
#include<stdio.h>
int main()
{
int choi;
printf("*****Interactive Calculator*****");
printf("\n\nChoose an option...");
printf("\n\n1. Addition\n");
printf("\n2. Subtraction");
printf("\n\n3. Multiplication");
printf("\n\n4. Division");
printf("\n\nPlease Enter your Choice : ");
scanf("%d",&choi);
if(choi==4)
{
float a=0,b=0,c=0;
printf("\nEnter Divident :");
scanf("%d",&a);
printf("\nEnter the Divisor :");
scanf("%d", &b);
c=a/b;
printf("\nThe Quotient is : %d\n\n",c);
char choice;
printf("Do you want to try it again?(Y/N) ");
scanf("%c", &choice);
// I want a code here so that the program can be restarted
getch();
return 0;
}
else
{
printf("\nErr#404-Invalid Character! Please Enter 1,2 or 3 !\n\n");
}
end:
getch();
return 0;
}
The best way would be to do some sort of a while loop.
int goAgain=1;
while (goAgain==1) {
... //Normal code here
printf("Again?")
scanf("%c",&again)
if (again=='N') {
goAgain=0;
}
}
Or you could use a do-while loop as well
do {
... //Normal code here
printf("Again?")
scanf("%c",&again)
} while (again=='Y')
Basically, this will keep looping over the bit of code over and over until the person types N to end it.
A do-while loop would be most suitable for this purpose.
int main() {
char choice;
do {
// Calculator stuff here...
printf("Do you want to try it again? (Y/N) ");
scanf("%c", &choice);
} while (choice == 'Y');
}
Edit: As it turns out, there is another problem with the program above, which is that scanf() reads a character but leaves a Newline character in the buffer. Therefore, if the user types YEnter, the program will repeat once (choice == 'Y' the first time), then exit (choice == '\n' the second time).
It is therefore necessary to keep reading until the Newline has been consumed.
int main() {
char choice;
do {
// Calculator stuff here...
printf("Do you want to try it again? (Y/N) ");
choice = getchar();
while (choice != '\n' && getchar() != '\n') {};
} while (choice == 'Y' || choice == 'y');
}
char continue = 'Y'
while (continue == 'Y') {
... //Normal code here
printf("Again?")
scanf("%c",&continue)
}
you can try like this, avoid go to
#include<stdio.h>
int main()
{
int choi;
while(true)
{
printf("*****Interactive Calculator*****");
printf("\n\nChoose an option...");
printf("\n\n1. Addition\n");
printf("\n2. Subtraction");
printf("\n\n3. Multiplication");
printf("\n\n4. Division");
printf("\n\n5. Exit");
printf("\n\nPlease Enter your Choice : ");
scanf("%d",&choi);
if(choi==1)
{
}
else if(choi==2)
{
}
else if(choi==3)
{
}
else if(choi==4)
{
}
else if(choi==5)
{
return 0; //exit(0);
}
else
{
printf("\nErr#404-Invalid Character! Please Enter 1,2,3,4 or 5 !\n\n");
}
}
return 0;
}
By using do-while loop, which is generally used for menu-driven programs.
#include<stdio.h>
int main()
{
int choi;
char choice;
do{
printf("*****Interactive Calculator*****");
printf("\n\nChoose an option...");
printf("\n\n1. Addition\n");
printf("\n2. Subtraction");
printf("\n\n3. Multiplication");
printf("\n\n4. Division");
printf("\n\nPlease Enter your Choice : ");
scanf("%d",&choi);
if(choi==4)
{
float a=0,b=0,c=0;
printf("\nEnter Divident :");
scanf("%d",&a);
printf("\nEnter the Divisor :");
scanf("%d", &b);
c=a/b;
printf("\nThe Quotient is : %d\n\n",c);
char choice;
printf("Do you want to try it again?(Y/N) ");
scanf("%c", &choice);
// I want a code here so that the program can be restarted
getch();
return 0;
}
else
{
printf("\nErr#404-Invalid Character! Please Enter 1,2 or 3 !\n\n");
}
printf("Want to continue (y/n)?");
scanf("%d", &choice); // Enter the character
}while (choice == 'y' || choice == 'Y');
end:
getch();
return 0;
P.S.: I would suggest you to use switch case, instead of if-else statements to do the job.
You can also use a goto:
int main() {
...
if (c=='y') {
main();
} else {
goto end;
}
end:
...
}
New to C still learning.
The program should start the first time with out needing to be asked to do anything. Then it prompts the user to continue with a "Y/N". I keep errors could anyone tell me why it doesn't work I don't know what to do with the errors that I get from it.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <ctype.h>
void theQnA(char charIn);
int main(void)
{
int answerint = 0;
char charIn;
char anwser;
printf("Enter a character to be examined: ");
scanf("%c", &charIn);
theQnA(charIn);
while (answerint == 0)
{
printf("Would you like to run it again? Y/N\n");
scanf("%c", &anwser);
if (anwser == 'y')
{
printf("Enter in another character buddy\n");
scanf("%c", &charIn);
theQnA(charIn);
}
else (anwser != 'y')
{
answerint = (answerint--);
}
}
printf("Goodbye\n");
return 0;
}
void theQnA(char charIn)
{
if (islower(charIn))
printf("You have entered in a lower case letter dude\n");
else if (isdigit(charIn))
printf("You enterd in a num man\n");
else if (isupper(charIn))
printf("Its upper case man\n");
else if (ispunct(charIn))
printf("You entered in a punctuation!\n");
else if (isspace(charIn))
printf("You enterd in a whitespace dude\n");
else
printf("control char/n");
return;
}
You have else (anwser != 'y'). It should be else if (anwser != 'y'), or better yet just else. The prompt Would you like to run it again? Y/N will also be printed twice because of how your loop is structured. You have quite a few mistakes, but here's some advice on your loop.
You can use your anwser variable in your while condition. answerint is unnecessary. Also, when you type a character and press enter, scanf (with %c) will extract the character but leave the newline in the buffer. That means the next call to scanf will return a newline, which will make it appear as if your program is skipping your input statements. To fix this, add a space before the %c in your call:
scanf(" %c", &charIn);
Your logic was also a bit out of place. Look at how this example is structured.
printf("Enter a character to be examined: ");
scanf(" %c", &charIn);
theQnA(charIn);
printf("Would you like to run it again? y/n\n");
scanf(" %c", &anwser);
while (anwser == 'y')
{
printf("Enter in another character buddy: ");
scanf(" %c", &charIn);
theQnA(charIn);
printf("Would you like to run it again? y/n\n");
scanf(" %c", &anwser);
}