C++ Help

Status
Not open for further replies.
1. Just because you won't ever use goto doesn't mean you won't use cin. cin is a very powerful input method and is used a lot.

2. That's because the rand() function doesn't actually generate random numbers properly. It has a set sequence that just sort of jumbles the numbers in the range around but is the same every time the application runs.

To debug your code just try this:

Code:
#include <stdlib.h>
#include <stdio.h>

int main()
{
	int i = 0;

	i = rand() % 11;
	printf("%d\n", i);

	return 0;
}

You'll see that every time you run the program the random number generated is the same.
 
Well thats a bit crap then :laughing:

Is there anyway round this ? for i really need truly random numbers..
 
That there is ;).

Code:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

int main()
{
	srand(time(NULL));
	
	int i;

	i = rand() % 11;
	printf("%d\n", i);

	return 0;
}
 
That there is ;).

Code:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

int main()
{
	srand(time(NULL));
	
	int i;

	i = rand() % 11;
	printf("%d\n", i);

	return 0;
}



Thanks. Now the only problem with that is, the only line i understand is:
i = rand() % 11;
:D Hmm ohwell, i'm sure i can figure out the rest somehow..

edit:

This may seem like a simple question but trust me when i say i've been trying for hours to get this to work using numerous methods.

What i want to do is to get the user to type in "yes or no" to a question. Such as

"Do you like chocolate ? , type Yes or No"

then if they type Yes, once block of code runs, if No, another one runs.

kinda like the following very basic example (which doesn't work, but shows what i mean)

Code:
#include <iostream>
using namespace std;

int main()

{

int answer;

cout << "do you like sushi ? type yes or no: ";
cin >> answer;

if(answer == yes) 
    { 
           cout << "Wow, you like sushi !";
    }
    else
    {
       
          cout << "Good, sushi's gross !";
     }

     return 0;

}

See what i mean ? Obviously the above code wouldn't work. But i've tried doing quite a few different things, but still can't get it to work. So how would i ?
 
That's because you're using answer as an integer. That way will only take the ASCII value of the first letter typed and nothing else. You need to use a string or the proper term is character array. You also cannot use the operator "==" for strings. You need use a function called string compare or "strcmp". This code here will work for you:

Code:
#include <iostream>
using namespace std;

int main()
{
	char answer[4];

	cout << "Do you like sushi? Type yes or no: ";
	cin >> answer;

    if(!strcmp("yes", answer))
    { 
	cout << "Wow, you like sushi!\n\n";
    }
    else
    {
	cout << "Good, sushi's gross!\n\n";
    }

    return 0;
}

If you don't understand anything I can go through it.
 
Ah, thanks alot man. I tried using char before but then my book said the char type is only big enough to store single letters and not words ?
I'll give that a try though, thanks again.
Btw, it must get pretty annoying me asking these stupid questions :eek: just tell me to shut up and go away if it gets on your nerves xD


edit:

I've just used goto in the following code, now i know everyone says it's horrible. But i really don't see the problem with it in the following situation. But if it really is bad, what else can i use ?

Code:
#include <iostream>
using namespace std;

int main()
{
	char answer[4];

	cout << "Do you like sushi? Type yes or no (or exit to quit): ";
	cin >> answer;

    if(!strcmp("yes", answer))
    { 
	cout << "Wow, you like sushi!\n\n";
    }
    else if(!strcmp("no", answer))
    {
	cout << "Good, sushi's gross!\n\n";
    }
	else if(!strcmp("exit", answer))
	{
		goto exit;

	}
	else
	{
		cout << "\n\n";
		cout << "Well if you can't even type yes or no..." << "\nhow do you expect me to trust your opinion ?";

	}

	cout << "\n\n\n";


	system("pause");

	exit:

    return 0;
}

May i also quickly add, there is no point in this code. I'm just simply trying things out, practice ;) And i added the system pause just so i can see whats going on before it insta-closes.
 
What compiler are you using? If it's Visual Studio or Visual C++ then you can press Crtl+F5 to run the program without the window closing instantly.

A char variable can only store one letter. That's why you see the number on the right side of it. That means that the variable "answer" can store up to four characters, also known as a character array. You need to make room for whatever your biggest string is going to be plus one extra. So in this case it would be 4 for "exit". The extra is for the null byte symbolized as "\0" to signal the end of the array.

Here is your code fixed up again:

Code:
#include <iostream>
using namespace std;

int main()
{
    char answer[5];

    cout << "Do you like sushi? Type yes or no (or exit to quit): ";
    cin >> answer;

    if(!strcmp("yes", answer))
    { 
	cout << "\nWow, you like sushi!\n\n";
    }
    else if(!strcmp("no", answer))
    {
	cout << "\nGood, sushi's gross!\n\n";
    }
    else if(!strcmp("exit", answer))
    {
	// empty if, nothing needed
    }
    else
    {
	cout << "\nWell if you can't even type yes or no..."
	       << "\nhow do you expect me to trust your opinion?\n"
	       << endl;
    }

    return 0;
}

As you can see the goto statement is completely uneeded. An empty if statement will simply end the entire if and go to the end of main. Don't worry about asking the questions. I'm here to help and glad to do it.
 
Oh yeh, silly me. I new that about the whole char[] thing anyway :p Was the previous chapter in my book, Arrays and Multidimensional arrays. Just need to remember it now.

I'm using Visual C++ 2008. Is there any better ones ?


Is there any function or way to skip to the end of the code - effectively quitting the application anywhere in the code ? whether it be at the start, inside a nested if, etc. Cant quite figure out how to quickly let the user exit the application using console input anywhere in the code (well, where i give the option to anyway). Not sure how clear that was, i'll try and make it clearer if your unsure.

This isn't particularly relevent, but do you know of any guides that show how to make very simple console based apps that are remotely ueful ? Because so far in my book it's taught lots of different things, but not how to use it to make a usefull application - which is kind of the purpose. Even something as simple as a program that can simply launch other programs, just by selecting the option in the console (that said, i actually have no idea how simple this really is)
 
I'm not really sure what you mean because the code above exits if they type exit. There is another method in which you put the code inside a "switch case break".
 
Yeh that exits in the code above, but that wouldn't work if i then had another load of code below that, such as if else if statements or whatever, would it ?
 
Status
Not open for further replies.
Back
Top Bottom