Jump to content

C++ Help

anagiotis567

So,I'm New To C++ and as a first project i made a little program called "Legal Driving" Which Basicly Checks If A Person Is Allowed To Drive By asking different question.I've been working on this for half an hour now, and i can't figure out what's wrong.The code runs but has some minor issues.You'll see.


#include <iostream>int option;int option2;int main(){ std::cout <<"                   Legal Driving                \n\n"; std::cout <<"Please,Answer The Following Questions\n"; std::cout <<"       1st Question\n      "<< "How Old Are You?\n"; std::cin>>option;if(option>='19')std::cout<<"You Are Legally Allowed To Drive";else if(option<='18')std::cout<<"You Are Forbidden From Driving";return 0; std::cout <<"       2nd Question\n      "<<"Is Your BAC Above 0.08?\n" or "Are You Drunk"; std::cin >> option2;if(option2 == 'Yes' or 'Probably' or 'Most Likely' or 'I Think So' or 'Of Course' or 'I ve had a drink or two')std::cout<<"You Are Forbidden From Driving";else if (option2 == 'No'or'Of Course Not'or'I Do Not Think So')std::cout<<"You Are Legally Allowed To Drive";return 0;}
Link to comment
Share on other sites

Link to post
Share on other sites

I'm not fluent in C++ per se, but it seems to me that you need an 'else' after the second 'else if' to counter the possibility that the input is something other than what you expected. It's good practice to always end all if-statements with user-provided values with an 'else' just in case they type in something unexpected, like in your case if the user typed 'yse' instead of 'yes'

 

So I'd put Else at the end and then reply something like "That's not a yes or a no. I'll just ask again." Then use GoTo to loop back to the row with the question. Maybe even clear the screen in between.

Link to comment
Share on other sites

Link to post
Share on other sites

You need {} with your ifs/else ifs/else if you are executing more than 1 statement. Your return statement is running regardless of the condition because you dont have parenthesis for your code blocks.

CPU: Intel i7 - 5820k @ 4.5GHz, Cooler: Corsair H80i, Motherboard: MSI X99S Gaming 7, RAM: Corsair Vengeance LPX 32GB DDR4 2666MHz CL16,

GPU: ASUS GTX 980 Strix, Case: Corsair 900D, PSU: Corsair AX860i 860W, Keyboard: Logitech G19, Mouse: Corsair M95, Storage: Intel 730 Series 480GB SSD, WD 1.5TB Black

Display: BenQ XL2730Z 2560x1440 144Hz

Link to comment
Share on other sites

Link to post
Share on other sites

Double posted from phone.

CPU: Intel i7 - 5820k @ 4.5GHz, Cooler: Corsair H80i, Motherboard: MSI X99S Gaming 7, RAM: Corsair Vengeance LPX 32GB DDR4 2666MHz CL16,

GPU: ASUS GTX 980 Strix, Case: Corsair 900D, PSU: Corsair AX860i 860W, Keyboard: Logitech G19, Mouse: Corsair M95, Storage: Intel 730 Series 480GB SSD, WD 1.5TB Black

Display: BenQ XL2730Z 2560x1440 144Hz

Link to comment
Share on other sites

Link to post
Share on other sites

Problem 1:

if blocks only run the next statement they see, so:

else if(option<='18')std::cout<<"You Are Forbidden From Driving";return 0;

does this:
 

else if(option<='18'){    std::cout<<"You Are Forbidden From Driving";}return 0;

Some formatting tips:

Don't write more than one statement per line.

// This is bad.DoSomething(); DoSomethingElse();// This is good.DoSomething();DoSomethingElse();

Problem 2:
 

Not sure what you're trying to do with this, but 'or' doesn't work like that.

std::cout <<" 2nd Question\n "<<"Is Your BAC Above 0.08?\n" or "Are You Drunk";

edit: Are you trying to write "Is Your BAC above 0.08\n?" sometimes, and "Are You Drunk?\n" sometimes?

 

If so, you need to randomly select between the two options, like this:

std::srand(std::time(0)); // use current time as seed for random generatorstd::cout << " 2nd Question\n "if ( std::rand() % 2 ) // Randomly selects 0 or 1, which evaluate to "False" and "True", respectively.    std::cout << "Is Your BAC Above 0.08?\n"else    std::cout << "Are You Drunk\n"

 

Note: % is the super-useful modulus operator.
https://simple.wikipedia.org/wiki/Modulo_operation

 

Problem 3:

Welcome to programming!  I think literally everyone has made this mistake, I certainly have:

if(option2 == "Yes" or "Probably" or "Most Likely" or "I Think So" or "Of Course" or "I ve had a drink or two")

The way 'if' statements work is this:

if (some_statement_that_can_either_be_true_or_false){    // You're only going to get here if the statement was true.    DoSomething();}

So, you need to boil down the stuff inside the parenthesis do either "true" or "false".  Now, in C++, the language writers decided that if the thing inside the quotes isn't a boolean, instead of throwing an error, it'll just treat most things like "true", and a few things like "false".  For integers, 0 is false, and everything else is true.  For strings, "" (the empty string) is false, and everything else is true (including the string "false"!).

A lot of the time, you're interested in the return value of a function.  So, you might have a program like:

if (isEven(2)){    std::cout<<"EVEN"}else{    std::cout<<"ODD"}

The equality operator ( "==" ) is a special kind of function, that they built into the language because doing something like:

if (isEqual(A, B)) {...}

is a lot uglier than doing:

if ( A == B ) { ... }

"or" is similar.  Instead of:
 

or(A, B, C) // Returns True if A is true, B is true, or C is true, and False otherwise

we do:

A or B or C

because that's nicer to write.

 

So, back to the problem with the line you wrote:

if(option2 == "Yes" or "Probably" or "Most Likely" or "I Think So" or "Of Course" or "I ve had a drink or two")

This is equivalent to:

if ( or(option2 == "Yes", "Probably", "Most Likely", "I Think So", "Of Course", "I ve had a drink or two") )

But since non-empty strings always resolve to "True", then 'Probably' always resolves to "True", so your if statement is always executed.

What you probably want is this:

if (option2 == "Yes"         or    option2 == "Probably"    or    option2 == "Most Likely" or    option2 == "I Think So"  or    option2 == "Of Course"   or    option2 == "I ve had a drink or two") // Multi-line if statements are ok in c++, and make it easy to read.{    DoSomething();}

Here's a reference for c++ conditionals:

http://www.cplusplus.com/forum/articles/3483/

 

Problem 4:

Don't use single quotes ( ' ) to specify strings, use double quotes ( " ).

 

 

One last thing:

Check out StackOverflow.com for programming questions.  There are a lot of great people there, and a lot of old answers to common questions.
Oh, and you may get annoyed with people being a dick to you because you're a beginner and people on StackOverflow are sometimes dicks to beginners.  They suck, don't get discouraged from using the site.

Link to comment
Share on other sites

Link to post
Share on other sites

--snip--

Is there any new standard or there has been made a mistake? I mean single quote strings, shouldn't they be inside double quote? Aren't single quote meant to be used as chars (even multibyte ones would be like max 4 characters long)? 

Link to comment
Share on other sites

Link to post
Share on other sites

@Mr_KoKa

Haaaaaaaaaaaa, whoops.  I've been using python lately.  Fixed.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×