Jump to content

Help with some basic coding

Spev
Go to solution Solved by h3Xx,

Also, when I run the code and use the number 20, it still works for 20, is there any way to make it only work for 19 and below? I can simply change the test to under 21....this is just a test program I'm creating, just for the principle and logic I'm trying to figure it out. I don't understand the || operator, an overload? Google is not my friend today I guess.

 

Edit: Figured this one out just changed the numbers a little bit lol it's been a long day.

 

This

FavNumber < 0 || FavNumber > 20

Translates to: (if favNumber is lower than 0) OR (if favNumber is higher than 20) go inside the loop. If you don't want the 20 to be included you can easily say (if favNumber is equal or higher than 20) which is FavNumber >= 20.

 

Now if the user enters a string the compiler is going to convert it to an integer which is not possible because an integer has 4bytes and inside a string every character is one byte. So you will end up with garbage values. Pay attention that there is a possibility that a string might give a garbage value inside 0 to 20 and your program will think it is correct but this possibility is very unlikely to happen.

I'm just trying to write a simple program in C++, yes it has to be C++.

 

I'm working on an if / else section. I have previously declared these variables. The program will ask the user to enter their favorite number under 20. If the number they enter is under 20 I want the program to assign that number to be equal to OfficialNumber, which I will reference later on in the program. If that user does not enter a number less than 20 as their favorite number I want the program to RE ASK the user to enter the number, I want this to keep repeating until the user finally enters a number that is under 20 thus allowing the program to continue. 

 

I know my else section is missing something. Need some help.

 

 

 

 

 

    cout << "What's your favorite number under 20?";
    cout << "\n";
    cin >> FavNumber;
 
if (FavNumber < 20)
    {
        OfficialNumber = FavNumber;
        cout << "Great! Next Question.";        //then I want it to progress to the next question.
    }
    else 
    {
        cout << "That's not a number under 20...try again please:";
        cout << "\n";
        cout << "What's your favorite number under 20?";
    }

Current PC build: [CPU: Intel i7 8700k] [GPU: GTX 1070 Asus ROG Strix] [Ram: Corsair LPX 32GB 3000MHz] [Mobo: Asus Prime Z370-A] [SSD: Samsung 970 EVO 500GB primary + Samsung 860 Evo 1TB secondary] [PSU: EVGA SuperNova G2 750w 80plus] [Monitors: Dual Dell Ultrasharp U2718Qs, 4k IPS] [Case: Fractal Design R5]

Link to comment
Share on other sites

Link to post
Share on other sites

Semicolon on the second last line and OfficialNumber = FavNumber. I may be wrong, I code Javascript

CPU AMD Ryzen 5 3600x | GPU GIGABYTE GTX 1070ti WINDFORCE | MOBO ASUS Prime X570-P | MEMORY XPG Spectrix 2x8GB 3200MHz  | PSU OCZ Zx 850W | CASE NZXT H440

Link to comment
Share on other sites

Link to post
Share on other sites

I have no idea how to code this in C++, but I know if you were to code in Java, I was taught that you would do this by looping until something like "numberUnder20 = True", then continuing, Idk if you can do that though.

Current Desktop Build | 2200G | RX 580 4GB | 8GB RAM | CTRL | Logitech G Pro Wireless

Laptop | 2018 MBA 256/16GB | MX Master 

Link to comment
Share on other sites

Link to post
Share on other sites

I have no idea how to code this in C++, but I know if you were to code in Java, I was taught that you would do this by looping until something like "numberUnder20 = True", then continuing, Idk if you can do that though.

I think you can I just haven't really learned about looping yet that's why I'm asking for help. 

Current PC build: [CPU: Intel i7 8700k] [GPU: GTX 1070 Asus ROG Strix] [Ram: Corsair LPX 32GB 3000MHz] [Mobo: Asus Prime Z370-A] [SSD: Samsung 970 EVO 500GB primary + Samsung 860 Evo 1TB secondary] [PSU: EVGA SuperNova G2 750w 80plus] [Monitors: Dual Dell Ultrasharp U2718Qs, 4k IPS] [Case: Fractal Design R5]

Link to comment
Share on other sites

Link to post
Share on other sites

Semicolon on the second last line and OfficialNumber = FavNumber. I may be wrong, I code Javascript

Uh yeah that's just an error on my part. It's not a problem with my program running that's an easy fix I'm just trying to figure out what to add at the end for my else statement.

 

Edit: Fixed 2 semicolon errors. Should be fine now.

Current PC build: [CPU: Intel i7 8700k] [GPU: GTX 1070 Asus ROG Strix] [Ram: Corsair LPX 32GB 3000MHz] [Mobo: Asus Prime Z370-A] [SSD: Samsung 970 EVO 500GB primary + Samsung 860 Evo 1TB secondary] [PSU: EVGA SuperNova G2 750w 80plus] [Monitors: Dual Dell Ultrasharp U2718Qs, 4k IPS] [Case: Fractal Design R5]

Link to comment
Share on other sites

Link to post
Share on other sites

you could use a do while loop to go back to the top if FavNumber isn't less than 20, just wrap the if else block in a do while loop with the condition of (FavNumber == 0 || FavNumber > 20).  (might need to check the logical operators its been a while since I've written c++).

2500k | Z68-UD3H-B3 | GTX 570 Classified + Accelero Xtreme III | MX 200 250GB + Seagate 500GB | R4 | Seasonic 660W Plattinum | 8GB G Skill | Acer K272HUL + 2 x Dell P2417H, Rosewill dual mount, HP 22bw | Mackie CR3, ATH-M50X | DAS Keyboard | Mobile: X1 Carbon, Nexus 6P, LG G3, Samsung S5, Nexus 7 (2013)

Link to comment
Share on other sites

Link to post
Share on other sites

some random while loop sample; adapt it to your needs

while (n>0) {    cout << n << ", ";    --n;  }

 

 

you could use a do while loop to go back to the top if FavNumber isn't less than 20, just wrap the if else block in a do while loop with the condition of (FavNumber == 0 || FavNumber > 20).  (might need to check the logical operators its been a while since I've written c++).

 

I apologize but if either of you could help me understand the while loop I would really appreciate it, I haven't used it before. I'm assuming it needs to start like this, but I don't know where to put this. Inside my else statement?

 

while (FavNumber >= 20)

{

 

?????

 

}

Current PC build: [CPU: Intel i7 8700k] [GPU: GTX 1070 Asus ROG Strix] [Ram: Corsair LPX 32GB 3000MHz] [Mobo: Asus Prime Z370-A] [SSD: Samsung 970 EVO 500GB primary + Samsung 860 Evo 1TB secondary] [PSU: EVGA SuperNova G2 750w 80plus] [Monitors: Dual Dell Ultrasharp U2718Qs, 4k IPS] [Case: Fractal Design R5]

Link to comment
Share on other sites

Link to post
Share on other sites

the while loop

the while loop will execute the code in between { } for as long as the condition inside ( ) holds true

sorry if I can't explain it better, I haven't coded in more than 15y

this site should be helpful with sample code: http://www.cplusplus.com/

Link to comment
Share on other sites

Link to post
Share on other sites

This would work best with a while loop:

while(FavNumber > 20 || FavNumber < 0){   cout << "Invalid number\n";   cin >> FavNumber;} 

If you can't or won't use one, you can use the goto statement to return to the start if the number is invalid.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

This would work best with a while loop:

while(FavNumber > 20 || FavNumber < 0){   cout << "Invalid number\n";   cin >> FavNumber;} 

If you can't or won't use one, you can use the goto statement to return to the start if the number is invalid.

 

if you want to be 100% correct it's better to use do..while to always enter first loop and avoid an uninitialized variable.

 

do{    cout << "Please give your favorite number under 20:" << endl;    cin >> FavNumber;} while (FavNumber < 0 || FavNumber > 20);
Link to comment
Share on other sites

Link to post
Share on other sites

 

if you want to be 100% correct it's better to use do..while to always enter first loop and avoid an uninitialized variable.

 

do{    cout << "Please give your favorite number under 20:" << endl;    cin >> FavNumber;} while (FavNumber < 0 || FavNumber > 20);

 

sure, I was assuming he had another line asking for a number before the loop - that way he could have the first sentence and then a dedicated "incorrect value" sentence

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

 

if you want to be 100% correct it's better to use do..while to always enter first loop and avoid an uninitialized variable.

 

do{    cout << "Please give your favorite number under 20:" << endl;    cin >> FavNumber;} while (FavNumber < 0 || FavNumber > 20);

Dude, that is awesome, I tried it in my code, I erase an entire if/else statement and a bunch of other mumbojumbo for these 4 lines and this basically did exactly what I needed it to do. If you could walk me through it I would greatly appreciate the logic behind this.

 

So the program prints the text and then asks for the user to enter FavNumber. I'm confused on the last statement. (FavNumber < 0 || FavNumber > 20), could you possibly explain how the compiler deals with this statement? What I'm trying to say as a beginner is I understand if/else statements really well. If is true and if the if statement isn't true the it's fault and runs the else code. I can see that logic on the screen through 2 different sections. However I don't understand this very well. Thank you for you help but I would be even more greatful if you could explain that line.

 

Also I am trying to fool proof my program. What if the user enters a number on accident? Is there a way to make the program re ask the question AGAIN if the user enters a number over 20 OR a letter or word?

Current PC build: [CPU: Intel i7 8700k] [GPU: GTX 1070 Asus ROG Strix] [Ram: Corsair LPX 32GB 3000MHz] [Mobo: Asus Prime Z370-A] [SSD: Samsung 970 EVO 500GB primary + Samsung 860 Evo 1TB secondary] [PSU: EVGA SuperNova G2 750w 80plus] [Monitors: Dual Dell Ultrasharp U2718Qs, 4k IPS] [Case: Fractal Design R5]

Link to comment
Share on other sites

Link to post
Share on other sites

 

if you want to be 100% correct it's better to use do..while to always enter first loop and avoid an uninitialized variable.

 

do{    cout << "Please give your favorite number under 20:" << endl;    cin >> FavNumber;} while (FavNumber < 0 || FavNumber > 20);

Also, when I run the code and use the number 20, it still works for 20, is there any way to make it only work for 19 and below? I can simply change the test to under 21....this is just a test program I'm creating, just for the principle and logic I'm trying to figure it out. I don't understand the || operator, an overload? Google is not my friend today I guess.

 

Edit: Figured this one out just changed the numbers a little bit lol it's been a long day.

Current PC build: [CPU: Intel i7 8700k] [GPU: GTX 1070 Asus ROG Strix] [Ram: Corsair LPX 32GB 3000MHz] [Mobo: Asus Prime Z370-A] [SSD: Samsung 970 EVO 500GB primary + Samsung 860 Evo 1TB secondary] [PSU: EVGA SuperNova G2 750w 80plus] [Monitors: Dual Dell Ultrasharp U2718Qs, 4k IPS] [Case: Fractal Design R5]

Link to comment
Share on other sites

Link to post
Share on other sites

Also, when I run the code and use the number 20, it still works for 20, is there any way to make it only work for 19 and below? I can simply change the test to under 21....this is just a test program I'm creating, just for the principle and logic I'm trying to figure it out. I don't understand the || operator, an overload? Google is not my friend today I guess.

 

Edit: Figured this one out just changed the numbers a little bit lol it's been a long day.

 

This

FavNumber < 0 || FavNumber > 20

Translates to: (if favNumber is lower than 0) OR (if favNumber is higher than 20) go inside the loop. If you don't want the 20 to be included you can easily say (if favNumber is equal or higher than 20) which is FavNumber >= 20.

 

Now if the user enters a string the compiler is going to convert it to an integer which is not possible because an integer has 4bytes and inside a string every character is one byte. So you will end up with garbage values. Pay attention that there is a possibility that a string might give a garbage value inside 0 to 20 and your program will think it is correct but this possibility is very unlikely to happen.

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

×