Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 04-05-2007, 09:16 PM   #1 (permalink)
 
True Techie

Join Date: Jun 2004

Posts: 125

Seraph is on a distinguished road

Default C++ program help

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.
Seraph is offline  
Old 04-05-2007, 11:47 PM   #2 (permalink)
 
Software Developer

Join Date: Mar 2006

Location: Columbus, OH

Posts: 569

jaeusm is on a distinguished road

Default Re: C++ program help

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.
jaeusm is offline  
Old 04-06-2007, 02:49 AM   #3 (permalink)
 
Junior Techie

Join Date: Apr 2007

Posts: 79

ZenOswyn is on a distinguished road

Default Re: C++ program help

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.
ZenOswyn is offline  
Old 04-06-2007, 07:20 AM   #4 (permalink)
 
True Techie

Join Date: Jun 2004

Posts: 125

Seraph is on a distinguished road

Default Re: C++ program help

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.
Seraph is offline  
Old 04-06-2007, 10:04 AM   #5 (permalink)
 
Software Developer

Join Date: Mar 2006

Location: Columbus, OH

Posts: 569

jaeusm is on a distinguished road

Default Re: C++ program help

Quote:
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.


Quote:
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;
}


Last edited by jaeusm; 04-06-2007 at 10:07 AM.
jaeusm is offline  
Old 04-06-2007, 11:32 AM   #6 (permalink)
 
True Techie

Join Date: Jun 2004

Posts: 125

Seraph is on a distinguished road

Default Re: C++ program help

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?
Seraph is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Best program for this lafguy81 Off Topic Discussion 2 04-04-2007 05:20 PM
Xbox 360 Disc Replacement Program Osiris XBox & XBox 360 5 04-04-2007 03:13 PM
one program, two computers benzimm86 Windows Operating Systems and Software 4 03-29-2007 11:41 AM
dead pixel fixer program... b1gapl Other Computer HW Topics 2 03-27-2007 11:16 PM
Best SlideShow Program rakoom2002 Windows Operating Systems and Software 2 03-23-2007 11:52 PM