234k views
1 vote
HW 1 / C Create a program "Guess My Letter" - All work can be done in the main method - pick a (your) letter and store it in a local variable - Get a char from the user - Compare the char submitted by the user with ‘your’ letter - keep requesting another letter from the user until you have a match (loop..?) - print some message identifying the match & exit on match * compile and run your program via gcc / mingw * submit the source ( .c ) file only

User Danbord
by
4.4k points

1 Answer

4 votes

Answer:

// This program is written in C++ programming language

// Comments are used for explanatory purpose

// Program start here

#include<iostream>

#include<stdlib. h>

using namespace std;

int main ()

{

// Create two char variables; myinput and userguess

char myinput, userguess;

// Accept input for myinput

cout<<"Pick a letter: ";

cin>>myinput;

// Initialize userguess to empty string

userguess = ' ';

system("CLS");

// Clear screen so that user won't see myinput. The content of the variable won't be cleared

// Prompt user to guess the letter correctly;

cout<<"Take a guess: ";

cin>>userguess;

// The iteration below will be repeated until the user guess correcto

while( userguess != myinput)

{

cout<<"You guessed wrongly\\"<<Take another guess: ";

cin>>userguess;

}

cout<<"You guessed right!!!";

return 0;

}

// End of Program

User Saamer
by
4.3k points