Please help, I cant seem to get the do-while loop to loop when I ask the user to input continue using scanf and printf. Could it be the wipe_buffer? I can't seem to get it right on this and I just need to finish the loop and that should be mostly functional. New to coding and this website please not too harsh. please and thanks.
#include <stdio.h>
#include <stdlib.h>
void wipe_buffer(void);
int main(int argc, char* argv[])
{
char play1;
char play2;
char cont;
do{
printf("Player one pick Rock, Paper, or Scissors\n");
scanf(" %c", &play1);
wipe_buffer();
printf("Player two pick Rock, Paper, or Scissors\n");
scanf(" %c", &play2);
wipe_buffer();
switch(play1)
{
case 'r':
if(play2 == 'p' || play2 == 'P')
{
printf("Paper Covers Rock\n");
printf("Player two wins\n");
}
if(play2 == 's' || play2 == 'S')
{
printf("Rock Breaks Scissors\n");
printf("Player one wins\n");
}
if(play2 == 'r' || play2 == 'R')
{
printf("Draw, Nobody wins\n");
}
break;
case 'R':
if(play2 == 'p' || play2 == 'P')
{
printf("Paper Covers Rock\n");
printf("Player two wins\n");
}
if(play2 == 's' || play2 == 'S')
{
printf("Rock Breaks Scissors\n");
printf("Player one wins\n");
}
if(play2 == 'r' || play2 == 'R')
{
printf("Draw, Nobody wins\n");
}
break;
case 'P':
if(play2 == 'p' || play2 == 'P')
{
printf("Draw, Nobody wins\n");
}
if(play2 == 's' || play2 == 'S')
{
printf("Scissors cuts Paper\n");
printf("Player two wins\n");
}
if(play2 == 'r' || play2 == 'R')
{
printf("Paper covers rock\n");
}
break;
case 'p':
if(play2 == 'p' || play2 == 'P')
{
printf("Draw, Nobody wins\n");
}
if(play2 == 's' || play2 == 'S')
{
printf("Scissors cuts Paper\n");
printf("Player two wins\n");
}
if(play2 == 'r' || play2 == 'R')
{
printf("Paper covers rock\n");
}
break;
case 's':
if(play2 == 'p' || play2 == 'P')
{
printf("Scissors Cuts Paper\n");
printf("Player one wins\n");
}
if(play2 == 's' || play2 == 'S')
{
printf("Draw, Nobody wins\n");
}
if(play2 == 'r' || play2 == 'R')
{
printf("Rock breaks Scissors\n");
printf("Player two wins\n");
}
break;
case 'S':
if(play2 == 'p' || play2 == 'P')
{
printf("Scissors Cuts Paper\n");
printf("Player one wins\n");
}
if(play2 == 's' || play2 == 'S')
{
printf("Draw, Nobody wins\n");
}
if(play2 == 'r' || play2 == 'R')
{
printf("Rock breaks Scissors\n");
printf("Player two wins\n");
}
break;
}
printf("Do you wish to continue?\n");
scanf(" &c", &cont);
wipe_buffer();
}while(cont == 'y' || cont == 'Y');
}
void wipe_buffer(void)
{
char t;
scanf("%c", &t);
while(t != '\n' )
{
scanf("%c", &t);
}
return;
}
In this section:
printf("Do you wish to continue?\n");
scanf(" &c", &cont);
"&c" should be "%c"
Note that when I compiled this with gcc, I got a warning that basically addressed this exact problem, so make sure to read your warnings.
In here:
printf("Do you wish to continue?\n");
scanf(" &c", &cont);
wipe_buffer();
}while(cont == 'y' || cont == 'Y');
there should be:
scanf(" %c", &cont);
Compile with -Wall -pedantic options next time you face some problem(if you use gcc).
It shows nicely where the problem lies, and if not - it narrows it down a little.
That's what it shows in your case:
test2.c:117:5: warning: too many arguments for format [-Wformat-extra-args]
test2.c:120:1: warning: control reaches end of non-void function [-Wreturn-type]
so it's also a nice reminder that your main() function has to return some value.
Another suggestion is to rad through this link:http://www.gidnetwork.com/b-60.html
and follow to The Steps page.
To make it short - there's some information on how time and space consuming is reading one char from stdin with scanf in comparison to getchar.
Consider applying this suggestion in your code.
So, a really quick way to see what's going on is to drop a debugging printf statement right before the loop
scanf(" &c", &cont);
wipe_buffer();
printf("Debug: [%c]\n", cont);
}while(cont == 'y' || cont == 'Y');
Then look for the printf when the program runs.
I think what is going on is that each scanf is looking for a Enter keystroke before processing, so wipe_buffer is going to request a lot of Enter keystrokes before continuing. Try running the program without the wipe_buffer and you'll still need to press Enter before it continues.
Related
image
I am working on a seat reservation program and here are some of its parts. My objective is to mark 'X' on the array using the user input. This is based on a tic tac toe program but I need to mark X only in the table. I am asking if I am doing the loop for the program correctly and if not what I use to fix it. I am facing errors but I don't know if this is the right loop for them.
I want to mark X based on the input of rows and columns that they are given and if it is already entered or marked. I want to say that it is already taken and will prompt the user again if they want to enter a new input again and if not they will be returned to the main menu again.
typedef struct{
char city[20], name[50], seatcol;
int age, seatrow, id;
}passenger;
char seat[ROWS][COLS];
void reserve(){
passenger p;
do {
printf("\n\t\t\tEnter your seat number:");
printf("\n\t\t\tROW:(1-10) ");
scanf(" %d",&p.seatrow);
printf("\t\t\tCOLUMN:(A-F) ");
scanf(" %s ", p.seatcol);
if(p.seatrow == 1 && p.seatcol == 'A')(
seat[0][0]= 'X');
else if(p.seatrow == 1 && p.seatcol == 'B')(
seat[0][1]= 'X');
else if(p.seatrow == 1 && p.seatcol == 'C')(
seat[0][2]= 'X');
else if(p.seatrow == 1 && p.seatcol == 'D')(
seat[0][3]= 'X');
else if(p.seatrow == 1 && p.seatcol == 'E')(
seat[0][4]= 'X');
else if(p.seatrow == 1 && p.seatcol == 'F')(
seat[0][5]= 'X');
//2
else if(p.seatrow == 2 && p.seatcol == 'A')(
seat[1][0]= 'X');
else if(p.seatrow== 2 && p.seatcol == 'B')(
seat[1][1]= 'X');
else if(p.seatrow == 2 && p.seatcol == 'C')(
seat[1][2]= 'X');
else if(p.seatrow == 2 && p.seatcol == 'D')(
seat[1][3]= 'X');
else if(p.seatrow == 2 && p.seatcol == 'E')(
seat[1][4]= 'X');
else if(p.seatrow == 2 && p.seatcol == 'F')(
seat[1][5]= 'X');
else{
printf("Invalid option!");
p.id--;
getch();
}
p.id++;
status = (p.seatrow && p.seatcol != seat);
}while(status);
if(!status){
printf("\n\t\t Already allocate seat. Choose another seat? (Y/N)");
scanf("%s", answer);
if(answer == ' Y'){
printf("\n\t\t\tROW:(1-10) ");
scanf(" %d",&p.seatrow);
printf("\t\t\tCOLUMN:(A-F) ");
scanf(" %s ", p.seatcol);
}
else{
printf("Your data will be not saved and will be returned to main menu:");
mainmenu();
}
}
}
I made a simple C program to understand the working of the If-Else statement but in VS Code the program stops at second input without any error prompt. Please tell me what's the problem with my program? I'm a beginner in programming.
#include <stdio.h>
int main(){
char math, sci;
printf("Have you passed Mathematics test (y/n)\n");
scanf("%c", &math);
printf("Have you passed Science test (y/n)\n");
scanf("%c", &sci);
if ((math == 'y') && (sci == 'y'))
{
printf("You get a gift of worth Rs. 45.");
}
else if ((math == 'n') && (sci == 'y'))
{
printf("You get a gift of worth Rs. 15.");
}
else if ((math == 'y') && (sci == 'n'))
{
printf("You get a gift of worth Rs. 15.");
}
else if ((math == 'n') && (sci == 'n'))
{
printf("You don't get any gift.");
}
return 0;
}
The second scanf() reads the newline that was left pending in stdin by the first scanf().
Use scanf(" %c", &sci); with an initial space in the conversion string to consume any newlines and initial white space in the input. Also test the return value of scanf() to detect premature end of file.
Here is modified version:
#include <stdio.h>
int main() {
char math, sci;
printf("Have you passed Mathematics test (y/n)\n");
if (scanf(" %c", &math) != 1) {
printf("Missing input\n");
return 1;
}
printf("Have you passed Science test (y/n)\n");
if (scanf(" %c", &sci) != 1) {
printf("Missing input\n");
return 1;
}
if ((math == 'y') && (sci == 'y')) {
printf("You get a gift of worth Rs. 45.\n");
} else
if ((math == 'n') && (sci == 'y')) {
printf("You get a gift of worth Rs. 15.\n");
} else
if ((math == 'y') && (sci == 'n')) {
printf("You get a gift of worth Rs. 15.\n");
} else
if ((math == 'n') && (sci == 'n')) {
printf("You don't get any gift.\n");
} else {
printf("Invalid input.\n");
}
return 0;
}
You just change
scanf("%c", &math) to scanf(" %c", &sci)
scanf("%c", &sci) to scanf(" %c", &math)
#include <stdio.h>
int main(){
char math, sci;
printf("Have you passed Mathematics test (y/n)\n");
scanf(" %c", &math);
printf("Have you passed Science test (y/n)\n");
scanf(" %c", &sci);
if ((math == 'y') && (sci == 'y'))
{
printf("You get a gift of worth Rs. 45.\n");
}
else if ((math == 'n') && (sci == 'y')||(math == 'y') && (sci == 'n'))
{
printf("You get a gift of worth Rs. 15.\n");
}
else if ((math == 'n') && (sci == 'n'))
{
printf("You don't get any gift.\n");
}
return 0;
}
I would like the user to write a letter for a selection, the problem is that it only detects lowercase letters and not uppercase, could you help me?
#include <stdio.h>
#include <ctype.h>
int main ()
{
char choice;
printf("Will you choose A, or B?\n>");
do {
scanf(" %c", &choice);
} while (choice != 'a' && 'A' && choice != 'b' && 'B');
switch (choice) {
case 'A':
case 'a':
printf("The First Letter of the Alphabet\n");
break;
case 'B':
case 'b':
printf("The Second Letter of the Alphabet\n");
break;
}
system("pause");
return 0;
}
In
choice != 'a' && 'A' && choice != 'b' && 'B'
'A' and 'B' are just interpreted as "true" -- the expression needs to be
choice != 'a' && choice != 'A' && choice != 'b' && choice != 'B'
A better alternative might be to move the switch into the loop, making sure the loop exit condition and the switch are consistent.
This question already has answers here:
if-else statement
(3 answers)
Closed 3 years ago.
Hi i am new to C i am writing a basic hangman game and when i run the code the else statement still triggers even if the if statement before it has triggered why does this happen
Here is the code :
#include <stdio.h>
int main(int argc, char const *argv[]) {
int Guesses = 0;
int Completion = 0;
char UserGuess;
char Word[] = "fire";
while (Guesses < 6) {
if (Completion == 4) {
printf("You Won\n");
break;
}
printf("Please Enter Your Guess It Can Only Be A Letter Not A Word\n");
printf("The Word Is 4 Characters Long\n");
scanf("%c", &UserGuess);
if (UserGuess == 'f') {
printf("You Guessed One Of The Letters\n");
printf("f_ _ _\n");
Completion++;
}
if (UserGuess == 'i') {
printf("You Guessed One Of The Letters\n");
printf("_i_ _\n");
Completion++;
}
if (UserGuess == 'r') {
printf("You Guessed One Of The Letters\n");
printf("_ _r_ \n");
Completion++;
}
if (UserGuess == 'e') {
printf("You Guessed One Of The Letters\n");
printf("_ _ _e\n");
Completion++;
}
else {
printf("You Guessed The Letter Wrong try again\n");
Guesses++;
}
}
}
Look at your if..else:
if (UserGuess == 'e') {
printf("You Guessed One Of The Letters\n");
printf("_ _ _e\n");
Completion++;
}
else {
printf("You Guessed The Letter Wrong try again\n");
Guesses++;
}
This will print "You Guessed The Letter Wrong" if you enter any letter besides 'e'.
You need to add an if..else..if chain for all your conditions:
if (UserGuess == 'f') {
printf("You Guessed One Of The Letters\n");
printf("f_ _ _\n");
Completion++;
} else if (UserGuess == 'i') {
printf("You Guessed One Of The Letters\n");
printf("_i_ _\n");
Completion++;
} else if (UserGuess == 'r') {
printf("You Guessed One Of The Letters\n");
printf("_ _r_ \n");
Completion++;
} else if (UserGuess == 'e') {
printf("You Guessed One Of The Letters\n");
printf("_ _ _e\n");
Completion++;
} else {
printf("You Guessed The Letter Wrong try again\n");
Guesses++;
}
I am trying to get this yes no programme to work in a loop. I've checked the other users messages and there's only one which is poorly written and doesn't work properly.
So if the user types y or Y it installs and if they type n or N it exits out of the program. Also if they type w, m or any other letter that isn't y or n it goes back to the start and asks them again.
Not sure if its a while loop or a do while loop. The programme below works but doesn't have any loops.
#include <stdio.h>
int main() {
char yn;
printf("Do you want to install this programme? y/n: ");
scanf("%c", &yn);
if(yn == 'y' || yn == 'Y') {
printf("Installing...\n");
}
else if(yn == 'n' || yn == 'N') {
printf("Exiting programme!\n");
}
else {
// Go back to the start/top of the programme!
}
return 0;
}
You can wrap your code into a while-loop.
Something like:
while(1)
{
printf("Do you want to install this programme? y/n: ");
scanf("%c", &yn);
if(yn == 'y' || yn == 'Y') {
printf("Installing...\n");
break; // Stop the while-loop to end the program
}
else if(yn == 'n' || yn == 'N') {
printf("Exiting programme!\n");
break; // Stop the while-loop to end the program
}
}
The type of loop that makes most sense in this scenario is a do/while loop since getting a response from a user is something that should happen at least once and be tested for until a desired response is obtained from the user.
Also, using tolower or toupper on yn when checking for equality can eliminate the need to check both upper and lowercase.
do
{
printf("Do you want to install this program? y/n: ");
scanf(" %c", &yn);
}
while(tolower(yn) != 'n' && tolower(yn) != 'y');
if(tolower(yn) == 'n')
{
printf("Exiting program\n");
}
else
{
printf("Installing ...\n");
}
fgets can be used to capture the input. It has an advantage of being able to clear the input stream in case of too many characters or incorrect characters.
#include <stdio.h>
#include <string.h>
int main ( void) {
char input[3] = "";//can hold one char a newline and a '\0'
printf("Do you want to install this programme? y/n: ");
do {
printf ( "\nenter y or n\n:");
if ( fgets ( input, sizeof input, stdin)) {
if ( !strchr ( input, '\n')) {//is there a newline?
while ( !strchr ( input, '\n')) {//call until newline is found to clear input
if ( !fgets ( input, sizeof input, stdin)) {
fprintf ( stderr, "\nEOF problem\n");
return 1;
}
}
input[0] = 0;
printf ( "\ntoo many characters. try again.");
}
}
else {
fprintf ( stderr, "\nEOF problem\n");
return 1;
}
if ( input[0] == 'y' || input[0] == 'Y') {
printf("Installing...\n");
}
if ( input[0] == 'n' || input[0] == 'N') {
printf("Exiting programme!\n");
}
} while ( input[0] != 'y' && input[0] != 'n' && input[0] != 'Y' && input[0] != 'N');
return 0;
}
Solved!
This is the code that works. Thanks to #govindparmar.
#include <stdio.h>
int main() {
char yn;
do {
printf("Do you want to install this programme? y/n: ");
scanf(" %c", &yn);
}
while(yn != 'n' && yn != 'N' && yn != 'y' && yn != 'Y');
if(yn == 'n' || yn == 'N') {
printf("Exiting programe!\n");
}
else {
printf("Installing...\n");
}
printf("It works!\n");
return 0;
}