Hello I just joined up this is my first post,
Let me give my background first, I actually just started, i'm real new to c/c++ programming, I was more decided to learn C++ (as projects i'm most interested in use C++ majority of the time including game programming) very recently but the only friends i have that are advanced experienced programmers that have C/C++ skills are C programmers, they sort of told me to try and start with C first for various reasons even if i end up just doing C++ anyways, like the issue that even if i wanted to just code C++ they said theres alot of code in the wild that are mixed with C anyways so knowing some C basics would be helpful for me to understand and it will also give me an idea of the difference of both languages form an opinion of them by myself,
so i just started C++ anyways and thought to why not check out C, and well i cant talk to them all the time since they are busy senior developers and they are much older with families even, one of them is into compiler design even, anyways so i thought to join this forum for information and advice when i cannot talk to them
the program i'm having issue with is actually a C program thats why i am posting on the C board, that i learned from a C++ program, I tried to translate from c++ to c, but cant figure out the problem the c program does something un-intentional theres supposed to be a loop to check user input with boolean value, to check if the user wants to quit or not, it never happens unlike in the c++ version which does, i found out that the boolean was just added in c99, i tried compiling for both c99 and c11 with including this line at the beginning even #include <stdbool.h>but im not sure im doing it right, its my suspicion that the problem is to do with boolean and how i'm implementing it, since the program compiles but just doesnt work the same as the c++ one
any advice or a fix much appreciated, its just a basic program - guess my number game
this is the C++ version
Code:
#include <iostream>
using namespace std;
int main() {
cout << "This is the Guess My Number Game!" << endl;
cout << "Please type in your number guess below! " << endl;
int answer = 7;
int guess;
char yesOrNo;
bool keepgoing = true;
while(keepgoing) {
cout << "Enter a Number: ";
cin >> guess;
cout << "You entered " << guess << endl;
if (guess == answer) {
cout << "Your Guess is 7, that's the correct number, you win!" << endl;
}
else if (guess < answer) {
cout << "Your guess is lower than the mystery number" << endl;
}
else if( guess > answer) {
cout << "Your guess is higher than the mystery number" << endl;
}
else {
cout << "i'm lost" << endl;
}
cout << "Play Again? (type y for yes or n for no)" << endl;
cout << "y or n? " << endl;
cin >> yesOrNo;
switch(yesOrNo) {
case 'Y':
case 'y':
keepgoing = true;
break;
case 'N':
case 'n':
keepgoing = false;
break;
}
/* if else
if (yesOrNo == 'y') {
keepgoing = true;
}
else if(yesOrNo == 'n') {
keepgoing = false;
}
else {
break;
*/
// (yesOrNo == 'y' || 'Y') ? keepgoing = true: keepgoing = false; // Ternary
}
return(0);
}
and the C version that doesnt seem to work the same way as the C++ one
Code:
#include <stdio.h>
#include <stdbool.h>
int main(void) {
printf("This is the Guess My Number Game!\n" );
printf("Please type in your number guess below!\n" );
int answer = 7;
int guess;
char yesOrNo;
//bool keepgoing = true; //tried this, and _Bool no luck
_Bool keepgoing = 1;
while(keepgoing == 1) {
printf("Enter a Number: ");
scanf("%d", &guess);
printf("You entered: %d\n", guess);
if (guess == answer) {
printf("Your Guess is 7, that's the correct number, you win!\n");
}
else if (guess < answer) {
printf("Your guess is lower than the mystery number\n");
}
else {
printf("Your guess is higher than the mystery number\n");
}
printf("Play Again? 'y' or 'n'?\n");
scanf("%c", &yesOrNo);
// yesOrNo = getchar();
//switch
switch(yesOrNo) {
case 'Y':
case 'y':
keepgoing = 1; //tried values 1 and True
break;
case 'N':
case 'n':
keepgoing = 0; //tried values 0 and false
break;
}
/* if else
if (yesOrNo == 'y') {
keepgoing = 1;
}
else if(yesOrNo == 'n') {
keepgoing = 0;
}
*/
//(yesOrNo == 'y') ? keepgoing = 1 : keepgoing = 0; // ternary
}
return(0);
}