Skip to content

Instantly share code, notes, and snippets.

@unChartedZone
Created April 13, 2016 03:29
Show Gist options
  • Select an option

  • Save unChartedZone/e7d425bc45b82141a51c288288e02bd8 to your computer and use it in GitHub Desktop.

Select an option

Save unChartedZone/e7d425bc45b82141a51c288288e02bd8 to your computer and use it in GitHub Desktop.
This is a c++ file of the classic game Fizz Buzz for children
//
// Created by Chris on 4/11/16.
//
#include <iostream>
#include <sstream>
using namespace std;
bool checkString(string string1) {
for(unsigned int i = 0; i < string1.length(); i++) {
if(isdigit(string1[i]) == 0)
return false;
}
return true;
}
int string_to_int(string s) {
istringstream instr(s);
int n;
instr >> n;
return n;
}
void start_game() {
string temp = "";
int counter = 1;
cout << "Enter 1 to begin or end to quit";
while(temp != "end") {
cout << ": ";
cin >> temp;
if(counter % 3 == 0 || counter % 5 == 0) {
if (temp == "fizz" && counter % 3 == 0) {
counter++;
continue;
}
if(temp == "buzz" && counter % 5 == 0) {
counter++;
continue;
}
else {
cout << "Sorry that's wrong! You were supposed to enter fizz or buzz, Play again!" << endl;
break;
}
}
if(!checkString(temp)) {
cout << "You didn't enter a number!" << endl;
break;
}
int num = string_to_int(temp);
if(num != counter) {
cout << "Sorry that's wrong. Play again!" << endl;
break;
}
counter++;
}
}
int main() {
start_game();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment