I just finished an exam that I bombed pretty hard. I just dont know where to begin on this stuff and would love some guidance. Below is the details of the exam.

- Write a program that asks a user to enter their full name. Their name including spaces must be stored in a single string. Declare the string with a maximum of 40 characters.
- Calculate the # of Characters (C) in the string holding the full name and generate a random number (bullseye) between 1 and C (seed random number generator with current CPU time).
- Send these numbers (C and bullseye) as input parameters to a function called Guess.
- Inside the Guess function ask the user to guess the bullseye number. If the user enters a number out of the range (smaller than 1 or larger than C), notify so and ask for the number again.
- Once the user enters a valid number (within the range), compare it to the bullseye number and return a value of 0 to the main function if the user guessed incorrectly or return a value of 1 to the main function if the user guessed correctly.
- Back in the main function, use the returned value from the Guess function to notify the user whether the guess was correct or not.
- Give the user 3 chances to guess the bullseye number.


The window should read like this...

Enter your full name:

Enter a guess between 1 and 15:

Your guess was incorrect

Another Chance...

Enter a guess between 1 and 15:

Your guess was incorrect

Another Chance...

Enter a guess between 1 and 15:

Your guess was incorrect

Press any key to continue...


Any help and advice is very appreciated.

Recommended Answers

All 7 Replies

Were is the program you wrote? Or did you answer the questions at all ? If you couldn't answer the test then you need to restudy your text book from page 1 and do all the extercies at tne end of each chapter. If you don't do this then you might as well drop the course and take up basket weaving.

I got hung up at the first statement where it says that the full name, including spaces, must be stored in a string. I got it to work up to the space, but after that it would not.

Here is the only code I got which doesnt even do anything. I broke the functionality it did have trying to figure out why the space in the full name wasnt working.

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>

using namespace std;

int main()
{
	char Fname[40];
	int L;
	cout << "Enter your full name: ";
	cin >> Fname;
	L = strlen(Fname);
	cout >> L.length();

	
system("PAUSE");
return 0;
}

int guess()
{
cout << "Enter a guess between 1 and 15:";

cout << "Your guess was incorrect\n\nAnother Chance...";
return 0;
}

You need to use cin.getline in order to include the spaces. Here's a tutorial on it.

http://www.cplusplus.com/reference/iostream/istream/getline.html


strlen is also a Cstring function, so instead of string, you would need to include string.h. Then after setting L = strlen(Fname), in your cout statement, you can just cout L, rather than L.length();.

Is this a C or a C++ program? The subject of your thread says C++ but the description of the problem says C everywhere. The answers to those questions will depend on which language is being used. For example: in C you would use fgets() to get an entire line including spaces, but in C++ you would use getline().

It is a C++ class. I just used (C) in the posting instead of the original letter that was in the assignment.

Try storing the name like this:

#include <string>

string str, name;

// Get string from user.
cout << "Enter your full name and press ENTER: ";
getline(cin, name);

//Build the output string, and print it.
str = "\nMy name is " + name + ".\n;

cout << str;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.