C++ program help

Status
Not open for further replies.

Seraph

In Runtime
Messages
125
I am just messing around with C++ right now as I want to take it in college. I am trying to make a program to help me study for a test. What I want it to do is to ask the question then say if it is right or wrong, then move on to a random question.

So the first problem I have is that I don't know how to do a String on Microsoft Visual C++. It doesn't recognize it as a proper data type. I am trying to do an if/else statement and it will not let me use any letters in the answer part of "if (question == answer)"

If I use a number as the answer then it works fine.


And then I don't know how to make it move on to a random question.
 
It would be far more beneficial for you to buy a good C++ book to help you learn. Everyone has his favorite, but one of best for beginners is "Accelerated C++" by Andrew Koenig. It takes a different approach than most books, but just read the reviews at Amazon, and you'll see it is highly regarded.
 
I don't think a book is required. For beginners, I recommend going to cplusplus.com - The C++ Resources Network and using it as a guide. If you need more examples, look up the topic on google. It's more of a reference site, but if you proceed in the order on the site, you'll have fewer problems later on.

Also, If you want to get a good answer, you should just post all of your code so we can get a better look at it.
 
I have a couple of C++ books. It is just hard to teach myself to do it when there is nobody in the town that I live in who knows jack about programming. I will be going to college in August anyways and learning it there, and I was told that they assume that students know nothing about C++ when they take the course, so it is a beginners course.

Here is the code. I don't know what some of it does (such as the include stuff.) I was just basing it off of some sample code from one of my books. I realize that the int main() part in the parenthesis is irrelevant to my code, but once again I was basing it off of a sample and it worked so I wasn't too worried.

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int main(int nNumberofArgs, char* pszArgs[])
{
char Q1;
cout << "How many years does a senator serve in Congress? ";
cin >> Q1;

if (Q1 == 6)
{
cout << "That is correct! ";
}
else
{
cout << "That is incorrect!";
}
system("PAUSE");
return 0;
}

So what I want to do is make the answer to the question "Six" instead of 6. Other answers will need more than one word for an answer. The code as it is right now works fine because I used a number.
 
I don't think a book is required.
While what you say is technically true in this instance, especially in light of the fact he will probably have a book in his college classes, that is horrible advice in general. Tutorials and online references are fine for a superficial level of understanding, which would more than likely suffice in this situation, but it's a bad habit to get into. I'm not suggesting that online info is worthless because it's not. It has its place and so do books.


So what I want to do is make the answer to the question "Six" instead of 6. Other answers will need more than one word for an answer.
You're on the right track, but first you should know that there is more than one way to accomplish your task. I would use a string (making sure to include <string>) and the 'getline' function. Modifying your code:
Code:
#include <iostream>
#include <string>
using namespace std;

int main(){
    string q1;
    cout << "How many years does a senator serve in Congress? ";
    getline(cin, q1);

    if (q1 == "six years")
        cout << "That is correct!";
    else
	cout << "That is incorrect!";

    system("PAUSE");
    return 0;
}
 
oh so all that I have to do is #include <string> and then string will be a valid data type?

do I have to do the getline command or can I just leave it as the cin >> Q1?


and then how can I make it move on to a random question?
 
Status
Not open for further replies.
Back
Top Bottom