Public Group

help with code

This topic is 5048 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

can someone help me figure out the problem with my "magic 8-ball program?" I have recently begunl earning c++ on gametutorials.com,and i am new to this code: #include <iostream> #include <string.h> int main() int i=1; int s=0; char aaa[50]={0} while (i=1) { input() } }return void; int input() { std::cout << " Enter your yes/no question "; std::cin >> aaa ; srand ( GetTickCount ( ) ); s=rand() % 3; switch(s) { case 1: cout << "yes"; break; case 2: cout << "no"; break; case 3: cout << "maybe"; break; } thanks for helping a newb you have my gratitude

Share on other sites
Change the while (i=1) to while (i == 1)
and return void; to return 0;

Not too sure about the rest since it's C++. I only know C:D

Share on other sites
#include <iostream>#include <string.h>#include <time.h>		// included for time() function, see belowint input();	// must tell compiler that a function called input() exists.				// alternatively, move the input-function here.int main(){			// missed initial bracket	int i=1;	srand ( time ( NULL ) );	// Replaced GetTickCount() with a call to time(NULL)								// as the header for GetTickCount was not included								// moved srand-call out of the while-loop								// you only need to seed the random generator once								// per run	while (i==1) // as mentioned, == compares. a single = assigns	{		input();	// missed ;	}	return 0;	// as mentioned}int input() {	char aaa[50]={0};	// missed ;	int s=0;			// moved declaration of aaa and s here, as the variables 						// are used in this function	std::cout << " Enter your yes/no question";	std::cin >> aaa;	s=rand() % 3;	switch(s) 	{ 	case 1: 		std::cout << "yes"; // forgot to declare namespace		break;	case 2: 		std::cout << "no"; 		break; 	case 3: 		std::cout << "maybe";		break;	}	return 0;		// as input was declared to return an int,					// we must return and int.}

That compiles on my setup. I've included verbous comments on the changes... ask if anything is unclear.
There are still a few issues...
* what happens when the random number stored in s becomes 0?
* how should the user exit the program?
* formatting (throw in a few newlines!)

Share on other sites
Your modulus with 3 ( % 3) will give you the numbers 0, 1, 2. Your switch should be checking for these values. The case for 3 will never fire.

Share on other sites
ah, thanks for all your help, you have furtered my fascination with
C++

Share on other sites
Your compiler can tell you most of these things. You can pick out a lot of simple typing errors for yourself as well, by picking a consistent indentation style.

Here I have taken your original code and made no changes except to move around the whitespace (indentation of lines and newlines), and made comments indicating the syntax errors. This indentation style is the one I prefer. It is not authoritative; there are several ways to do it (and actually some of the things I do are pretty unusual), but the important thing is being consistent.

#include <iostream>#include <string.h>int main() // missing open bracket  int i=1;  int s=0;  char aaa[50]={0} // missing semicolon  while (i=1) { // Should be == instead of =    // although it doesn't happen to matter in your case    input() // you don't *need* a semicolon here but most people    // would put one.  }  // You don't actually have to write a return value here :)  // This is a special case for the main function, it returns  // 0 if it gets to the end without a problem. This is the  // return value you want; it signals "no error" back to the  // operating system. The whole idea of a *program* returning  // something is basically a UNIX idea; it lets you write  // scripts that call programs (and take corrective action if  // there was a problem running the program).}return void; // in the middle of nowhere!// Also, "void" is not a valid return value.int input() {  std::cout << " Enter your yes/no question ";  std::cin >> aaa ; // "aaa" is not defined here but in the  // main function, so it's not a valid name here.  // You want to define it in this function instead.  srand(GetTickCount());  s=rand() % 3; // Ditto for "s".  switch(s) {    case 1:      cout << "yes";      break;    case 2:      cout << "no";      break;    case 3:      cout << "maybe";      break;  }// Missing close bracket at the end; the one you have there// just closes the switch statement.

There are of course other logical problems (the other posters pointed out... probably most of them), and style problems, but neither of those will prevent the program from *compiling*, which is the first step.

EXERCISE 1: If you haven't done so already, read the board FAQ and learn how to format code properly for posting.

EXERCISE 2: Feed the above code back to your compiler, and *read the error messages*. See how they correspond to the lines I've indicated, and what error message you get for what kind of problem. Consult a dictionary if necessary - the compiler *does* "speak English", but it sometimes (a) has a very different idea than you about what you're *trying* to do, because it can't think or read minds; (b) uses strange technical jargon.

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5
frob
12

• 9
• 22
• 16
• 9
• 17
• Forum Statistics

• Total Topics
632608
• Total Posts
3007396

×