#I need help to understand how to do these problems with while loops
1912 messages · Page 2 of 2 (latest)
You ruined my day
int ans = 196;
int attempts = 4;
int guess;
cout << " I am a three-digit number.My tens digit is five move than my ones digit,and my hundreds digit is eight less than my tens digit." << endl;
cout << "What number am I? ";
while(attempts != 0) {
cin >> guess;
cout <<"You've run out of attempts.Bye!"<< endl;
attempts -= 1;
if(guess== ans) {
cout <<" Well Done!";
}
if(attempt==0) {
cout << ans;
break;
}
}
return 0;
}```
so like this?
damn
If I guess correctly on first try, it will annoyingly continue to ask for input 3 more times
Check this out
what is std:: cout << riddle
And cin cannot pass to guess, because the input is a string, you need to convert it to int first
riddle is a string variable
I print the string riddle out
so it essentially means "output the riddle variable"
oh thats what std mean
Why did u take the break outside your if statement for if you get it correct?
no, cout means output
std is just a namespace, aka a sort of library/collection of functions and other stuff
Because what the user types in is a string, a string isn't an integer
You need to convert this string to an integer first
This can be done with std::stoi(str)
No no
stoi = String To Integer
There is no need to convert any string to a integer in this program
And that is way too advanced too
For their stage
So, instead, we'll just change to "std::string ans = "196""?
This program just wants to check if the user input is equal to 196
Two numbers
That’s it
We actually are working with one string and one integer
Where do u see string?
Is there implicit type conversion (int -> string) when we compare string and int?
alright so i change the ans to a string instead of a int?
std::cin doesn't give you an integer, right?
Or am I being dumb
it gives numbers
Cin doesn’t give u anything?
the user is supose to give numbers
It just returns false or true if the value read is valid
There’s no need of strings anywhere
We are just working with integers
What's the definition of 'valid' here?
I'm confused, please hang onto me for a second
If the data type read from the cin stream is equal to the data type being changed
Such as
Int value;
cin >> “string”;
This is off topic though
Just look up what cin returns
Anyways what step are u on again?
who me?
or the other person
uh i ma just assume u mean me
int ans = 196;
int attempts = 4;
int guess;
cout << " I am a three-digit number.My tens digit is five move than my ones digit,and my hundreds digit is eight less than my tens digit." << endl;
cout << "What number am I? ";
while(attempts != 0) {
cin >> guess;
cout <<"You've run out of attempts.Bye!"<< endl;
attempts -= 1;
if(guess== ans) {
cout <<" Well Done!";
break;
}
}
return 0;
}```
so apparently i have to fix the attempts thing
your code is going to produce the "run out of attempts" thing anyway
because you didn't add a condition around it
it's guaranteed to output it
@obtuse plover i'll get back to ur question later
after doing cin >> guess, the next line will output the "run out of attempts" message anyway
does that make a difference
so while( attempts !=0) is largely irrelevant
yeah
so i should put the attempts- before the cout statement
you're basically saying, "please input your answer", then before checking if the answer is correct, you're outputting the message that they've ran out of attempts
ohh
you don't need 2 while loops
that just complexifies the branching logic unnecessarily
even if it's just for a validation sanitisation
How will you simplify it?
i might have to do that attempts thing with a if statement then 🤔
or i could modify the condition in the while?
maybe add something
liek a ||
also if (attempts <= 0) should be if (attempts == 0) instead
it'll never reach under 0
plus it's simpler to understand the condition
oh alright
int ans = 196;
int attempts = 4;
int guess;
cout << " I am a three-digit number.My tens digit is five move than my ones digit,and my hundreds digit is eight less than my tens digit." << endl;
cout << "What number am I? ";
while(attempts == 0) {
cin >> guess;
cout <<"You've run out of attempts.Bye!"<< endl;
attempts -= 1;
if(guess== ans) {
cout <<" Well Done!";
break;
}
}
return 0;```
}
for the sanitisation, i'd do:
std::cin >> guess;
if (guess = "") {
std::cout << "invalid input, try again";
return 1;
}
it's much more intuitive than while (!cin >> guess)
wait i had !=
why would ==
be better
I learnt a lot from this alone, thanks
I never knew I can return 0 or 1 inside a while loop to either force it to close or to work like a "continue" in C#
That's so great to know
yknow what wait i'll remake @obtuse plover's code
ye
kernel, just to verify, when you return 1, it skips to the next loop without checking the conditions?
#include <iostream>
int main()
{
const int ans = 196;
int attempts = 4;
bool win = false;
std::string riddle = " I am a three-digit number.My tens digit is five move than my ones digit,and my hundreds digit is eight less than my tens digit. \n What am I? \n";
std::cout << riddle;
do
{
int guess;
std::cout << "What's your guess?\n";
cin >> guess;
if (guess == "")
{
std::cout << "Invalid input. Try again!\n";
}
if (guess == ans)
{
win = true;
std::cout << "You won! Congrats!";
return 0;
}
else
{
attempts -= 1;
}
} while ((win == false) && (attempts > 0));
std::cout << "you've ran out of attempts, try again next time";
return 0;
}
it should be something like this
You missed the Run out of attempts message
also i didn't take into account the string input validation between with the int but i left that out for the purpose of explanation
no it's at the end of the code
I'll get that message even if I win?
you won't
alright so bool works like a if statement right?
actually wait
expect u can just write false or true
instead of equations
to make it mean true or false
#include <iostream>
int main()
{
const int ans = 196;
int attempts = 4;
bool win = false;
std::string riddle = " I am a three-digit number.My tens digit is five move than my ones digit,and my hundreds digit is eight less than my tens digit. \n What am I? \n";
std::cout << riddle;
do
{
int guess;
std::cout << "What's your guess?\n";
cin >> guess;
if (guess == "")
{
std::cout << "Invalid input. Try again!\n";
continue;
}
if (guess == ans)
{
std::cout << "You won! Congrats!";
return 0;
}
else
{
attempts -= 1;
}
} while (attempts > 0);
std::cout << "you've ran out of attempts, try again next time";
return 0;
}
there
even better
this should be good now
Actually, for example (3 > 2) returns True, so if works with True not with (3 > 2) itself (what I think)
oh alright
Threw an error
you can write expressions and true or false
what part
/tmp/sqvGJ78dxG.cpp:17:15: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
17 | if (guess == "")
| ~~^
hmm instresting the while statment is completly at the end
i said it wouldn't work
¯_(ツ)_/¯
i thought that needed to be set for so the code knows what number it can go to
It's a do-while loop
You use it when you want to ensure the content inside the loop always runs at least once
It runs through the codes, then check if the condition is true, then it'll repeat the loop, else it continues with next lines
ah okay
can it be done with just a while loop first
or because if i use a while loop first, it causes more issues?
it can be done with a while loop or a do while loop
either is fine
personally i'd pick a do while loop for this situation
alright i'll put the if statements first
BTW, return 0 and return 1 didn't work how I expected it to
It returns 0 and 1 to main(), not the loop, like my speculation
wait but if i wanted to do with the while loop, how would it have to go
yeah because it can directly return to stdout
it doesn't need to reach the loop
it's more of a shortcut in the logic branch
so it can directly quit the program
@sleek halo fyi, the code i showed doesn't work on purpose
it was a demonstration of the logical path
that it should take
so the logical path being starting with if statements first
which if statement
there's 2 in the code
ok
ur saying it was more logical to do if statements first?
in my opinion yes
but i'm pretty sure my prof wants me to do it with a while
well is say "A "while loop is required
so as long as one is there
its probably fine im assuming
u know what i'm a beginner anyway, i should do what's simplier
if you don't understand something about the code, tell me
alright i don't know how bool works
never used it, my prof only did stuff with if statements so far
do you know what a type is in c++
yeah stuff like bool
mhm
char
int
double
there's also float but i think its less accurate than double
but double is used more
a bool is essentially just a true of false statement. you can do stuff like:
bool test = (1 + 10 == 11);
bool test2 = false;
expressions like ```cpp
if (10 == 10)
it's just a logical expression that's either true or false
you can also use in this way:
bool test = (10 == 10);
if (test) {
std::cout << "expression is true";
} else {
std::cout << "expression if false";
}```
hopefully that clears things up
and if statement can only take true or false
nothing else
and bool can do more than that
wdym
so u said if statemenrs can only take true or false
yeah pretty much
you can't do ```cpp
if ("something")
it MUST be a true or false statement
nothing more, nothing less
well technically this isn't true but for the sake of learning, think of it that way
bruh this internet
it can't do much
Oh alright
So u can do bool something
And later say if it’s true or false
Alright
can you give me an example of what you mean
yep
ping me when ur done
Ye
#include <iostream>
#include <string>
bool isPosNumber(std::string str)
{
for (int i =0; i < str.length(); i++)
{
if (!(isdigit(str[i]))) { return false; }
}
return true;
}
int main()
{
const int ans = 196;
int attempts = 4;
bool win = false;
std::string riddle = " I am a three-digit number.My tens digit is five move than my ones digit,and my hundreds digit is eight less than my tens digit. \n What am I? \n";
std::cout << riddle;
do
{
int guess;
do
{
std::cout << "What's your guess?\n";
std::string _strGuess = "";
std::cin >> _strGuess;
if (isPosNumber(_strGuess))
{
guess = std::stoi(_strGuess);
break;
} else
{
std::cout << "Invalid Input!\n";
}
} while (true);
if (guess == ans)
{
win = true;
std::cout << "You won! Congrats!";
}
else
{
attempts -= 1;
std::cout << "That's not right. Try again. \n";
if (attempts <= 0)
{
std::cout << "Sorry, you ran out of attempts.\n";
}
}
} while ((win == false) && (attempts > 0));
}```
I finally found a version that works
wait i'm confused where bool is being used? u declare win as false here? but where is it being used in the rest of the program??🤔
oh right
@normal juniper
if (guess == "")```
also this part i dont get
its supose to be wrong but u just put qutoes
i'm gonna continue working on this tomorrow
for now i gotta go
Why’s that? Is it because there’s also an error msg?
And also how does
if (guess = “”) work? Isn’t that nothing in the quotations?
This is the polished version of your code that u wrote earlier btw
int ans = 196;
int attempts = 4;
int guess;
cout << "I am a three-digit number. My tens digit is five more than my ones digit,and my hundreds digit is eight less than my tens digit." << endl;
cout << "What number am I? ";
// Loop until we run out of attempts
while(attempts != 0) {
cin >> guess;
attempts -= 1;
// If guess is the answer, congratulate and break out of the loop
if(guess == ans) {
cout <<"Well Done!";
break;
}
// If attempts is zero, print that you ran out of attempts
// then the while loop will exit itself right afterwards because of the condition
else if (attempts == 0)
cout <<"You've run out of attempts.Bye!"<< endl;
// When the guess is wrong and there are still attempts left, then this else will run
else
cout << "Wrong answer, try again: ";
}
return 0;
}```
And here's version 2 for learning purpose of learning "continue" because it's the counterpart to "break" and people usually learn it together:
int ans = 196;
int attempts = 4;
int guess;
cout << " I am a three-digit number.My tens digit is five move than my ones digit,and my hundreds digit is eight less than my tens digit." << endl;
cout << "What number am I? ";
// Loop until we run out of attempts
while(attempts != 0) {
cin >> guess;
attempts -= 1;
// If guess is the answer, congratulate and break out of the loop
if(guess == ans) {
cout <<"Well Done!";
break;
}
// If attempts is zero, print that you ran out of attempts
// then the while loop will exit itself right afterwards because of the condition
if (attempts == 0) {
cout <<"You've run out of attempts.Bye!"<< endl;
// continue is similar to break where you use it inside of a loop
// and it makes the program jump back up to the loop immediately
// so that no code below the continue will run
continue;
}
// This will print "wrong answer" every time when the two above if statements weren't executed
// because both above if statements will either break out of the loop or jump back up to the loop, so this code won't be reached unless both are false (AKA - isn't the right answer and isn't out of attempts)
// so, no need for an "else"
cout << "Wrong answer, try again: ";
}
return 0;
}```
Both of these versions work btw, i tested it. U can delete all the comments out if u want
sorry, the win variable isn’t necessary in my code
that’s supposed to be an input validator, it checks if the user input something and didn’t just press enter without anything typed
yes, the logic is "if the user inputs nothing, throw an error"
it's arguably not required though
How can the user enter nothing tho, wouldn’t it just be a \n
it's not a \n, if they input nothing then it's treated as byte 0x00
So they just press enter and the \n gets ignored?
Im just saying the \n since u have to press enter to use cin
But since it’s an integer then it’s just nothing in there 0x00
Is that what u mean?
this is how i'd do it
pretty much
unless i'm misunderstanding something
Does that even work tho? I feel like I remember if I press enter without typing anything, then the program doesn’t do anything it just waits until I actually enter something
it does
Oh
if nothing is inputed, the literal string for guess is just empty
and the whitespace is treated as null
And if it’s an integer then it won’t do anything until I enter something?
the sanitiation condition simply checks if it's empty
Yeah
wdym exactly
I swear I could’ve remembered that if I have a cin >>
And then I just press enter, it doesn’t do anything until I actually type something else and then enter
that's not how cin works
I guess I remember it wrong
Oh
Where’d u get that info then? About the null
Oh okay lol
I guess it’s understandable since ur proficient and I’m still a beginner so i remember still lol
it's mostly an input validation technique i got from non-cin io stuff
i thought it would work the same way
but apparently not
i'll edit the code btb
brb
#include <iostream>
#include <string>
using namespace std;
int main()
{
const int ans = 196;
int attempts = 4;
bool win = false;
std::string riddle = " I am a three-digit number.My tens digit is five move than my ones digit,and my hundreds digit is eight less than my tens digit. \n What am I? \n";
std::cout << riddle;
do
{
std::string guess = "";
std::cout << "What's your guess?\n";
std::cin >> guess;
if (guess == std::to_string(ans))
{
std::cout << "You won! Congrats!\n";
return 0;
}
else
{
std::cout << "Wrong answer, try again\n";
attempts -= 1;
}
} while (attempts > 0);
std::cout << "you've ran out of attempts, try again next time\n";
return 0;
}
there
@sleek halo sry for another ping but this is how i'd do it fr this time
It’s really interesting how u do a return 0 inside the loop, never seen that before but then again I’ve never seen professional work
i'm nowhere near professional
loops are pretty much irrelevant to return, since it's directly responsible for returning a function's value
it has no effect to loops other than immediately breaking it and discarding the rest of the code within the function
ooh
I see
it's sorta like instead of using a knife to cut open a watermelon and have clean slices in a procedural and repetitive cutting motion, you'd bring out a shotgun and blow the watermelon to pieces
that's how return is used in the code i showed
LMAO
it's probably a shit analogy but both methods work, it's just a difference of how direct it is at solving a problem
That’s a good comparison LOL
So do u see that method often in professional code?
Or it just depends
i'd say it's more on the professional end
cuz it minimises the branching and logical paths it has to take when the answer is already known
How are you 19 and already a systems programmer
i traded my irl social skills for programming skills

Im 20 going on 21 this year and I barely know shit… mostly because I need to work harder lol
Easiest trade tho
I just supplement with my animal skills lol
it's not something i necessarily brag about
it just shows i have no social life and spent half my life sticking my face on a computer
Your work ethic is something I envy though so be happy lol
that's still super nice
Def
What drives you though? Do you just love the work that much or, you have a goal in mind like I do
i only treat it like a hobby, programming is fun
i also figured it would open up a new field for me, so i put all my effort into learning how to code
and start making stuff i was intrigued about
Niceee
Like what for example?
CPU stuff, hardware tinkering, and some random shit like a password generator when i first started
i have a few failed projects tho
but it doesn't matter cuz i learned tons of new stuff anyway
i'm confident i can get one even without a degree
Definitely
a lot of programming jobs allow employees without a degree anyway
even apple does it
it's mostly about individual ability rather than what you did at university for 3 years
that's why i never took a CS degree
Exactly what I was thinking
And why I didn’t go for a degree either.. mostly because it’s so fucking expensive for so much time and unwanted classes
lol yeah
Though every time I look at jobs, just to see what’s out there, they all require a degree. I’m thinking I’m not looking in the right place. Where do u look? Linkedin?
Ik but u know where to look don’t u?
not rly icl
Oh nvm then

I didn’t know this tho lol
a certificate is mostly what would be recommended though
especially in cybersec jobs
like cybersecurity in uni is absolute dogshit, it's even worse than how CS is taught
I actually just finished a certificate this month for C/C++ programming by UCSD - extended studies
I feel like I didn’t learn nearly enough as I should’ve tho
whoa
congrats
oh
I mean it wasn’t nothing, it was definitely hard
But I just don’t have enough knowledge as I should
That sucks
it's all input at the end
you'd learn cybersecurity 100x more efficiently if you learned it yourself at your own pace
it's depressing
Hm, uni really seems like a scam nowadays
i like how this thread just ended up as a casual convo lol
And it’s interesting to know that it’s no different where you live as well
As it is in the US ^
Yeah I was thinking the same LOL
maybe yeah for CS
but other than that, there's loads of value
yeah
What for?
LOL
it has fuckall to do with programming
ty
So what kind of work would u want to do for them?
Hopefully u won’t have issues learning… whatever it is you learn in geopolitics
Good luck with that lol
thanks lol
Oh yeah, and why c++?
i’m a masochist
Alright I chnaged my one to it
#include <iostream>
using namespace std;
int main () {
int ans = 196;
int attempts = 4;
int guess;
cout << " I am a three-digit number.My tens digit is five move than my ones digit,and my hundreds digit is eight less than my tens digit." << endl;
cout << "What number am I? ";
while(attempts != 0) {
cin >> guess;
attempts -= 1;
if(guess == ans) {
cout << "Well Done!";
break;
}
else if(attempts == 0)
cout <<"You've run out of attempts.Bye!"<< endl;
else
cout <<"Wrong answer, try again: ";
}
return 0;
}
~ ```
it seems so obvious when someone else writes out some of the code that thats how the problem should be solved
but still its bad that I couldn't get to this point on my own
I know I've only been learning coding for 4 weeks now, but I'm definetly screwed for my upcoming Exam if I'm like this man 🥲
so anyway it runs and stuff
So now I just need help with my 3rd question
so 4 digit number so I probably have to set a while to 10,000 at max im assuming
I'm not too sure if i should do a if statement
this is what i have so far
Haha u know, it’s okay because that’s exactly how programmers will feel especially someone as new as you, that’s why we sent all of it complete because we know how it goes
#include <iostream>
using namespace std;
int main () {
int x = 1000;
cout << "I am a four-digit number.My first digit is one-third of my last digit,and the sum of my first two digits is equal to my thrid digit.What number am I?";
while(x < 10000) {
x = x + 1
if(x%10
yeah thanks for that
even if i'm not understanding, at least the hw is done at least i supose but yeah i'm trying my best to understand
so currently I need to make the if statements work
Whelp looks like ur gonna have to do some math for this one I think
yup
Yeah prob
I’ll get back to u in a bit gotta afk
so far i have the max number it should stop at 9999 and hopefully print all those numbers in between after then math
yeah sure
@sleek halo Has your question been resolved? If so, run !solved :)
okay so this is what i have so far
#include <iostream>
using namespace std;
int main () {
int x = 1000;
cout << "I am a four-digit number.My first digit is one-third of my last digit,and the sum of my first two digits is equal to my thrid digit.What number am I?";
while(x < 10000) {
x = x + 1
if(x%10 == 3 * x%10000 && x%10000 + x%1000 == x%10) {
cout <<"x";```
when your back, lemme know what you think?
Awesome, you already got the entire loop logic made. That’s huge progress since yesterday
Don’t forget the space after the x though in the cout
Uh for the math idk how to do I’d have to think on it
But yeah it’s probably something like what you did with the % and getting digit’s places
@sleek halo u there?
Yeah sorry I was really tired today so I fell asleep just a moment ago but I’m up now
That’s good to hear then
On Monday night
After I finish this though, I gotta study for the quiz for this since I have that on Tuesday for it
Also I saw a lot of these numbers have the same difference of 110, so if u can’t figure out the equation maybe u could use that instead
Oh damn what’s the quiz about
It’s all the way from the beginning to while loops
And then the exam is a week after that
Whihc yeah problems like these and that one is up to four loops or maybe the nested loops of she does a lesson on that next class
Yeah everything is going to fast
Wdym
The modules?
Oh yeah
I should have looked at the numbers more
But yeah I’ll probably have to include that in the math then
I think the equation might be easier
Yeah since it gives the same thing
Yesh
Yeah ur right
Alright so hopefully the program should run
But I might be missing a few things I’m not too sure
Oh right maybe the break statement
But I set a limit in the while
To 10000
So I’m hoping it would stop there
Well I guess I should run it first then
Yeah it should stop
Ah but it doesn’t print the numbers right
#include <iostream>
using namespace std;
int main () {
int x = 1000;
cout << "I am a four-digit number.My first digit is one-third of my last digit,and the sum of my first two digits is equal to my thrid digit.What number am I?";
while(x < 10000) {
if(x < 10000) {
x = x + 1;
if(x%10 == 3 * x%10000 && x%10000 + x%1000 == x%10) {
cout << x;
}
}
}
return 0;
}```
do u know which is wrong then?
Tbh what u should do is comment out ur entire code, and just focus on making the equation and test it just once until u figure out the equation
Uh no I don’t
But if ur too tired to focus just sleep
yeah I probably should
Cya later
1466 messages wth

hey guys
if(x < 10000) {
x = x + 1;```
could it be because of this statement?
since i'm repeating what i said in the while condition in the if condition
nvm it just didn't run when i tried it
Crazy, what are you talking about with so long history tbh .
Did u get any error
There’s a screenshot of the question up somewhere lol
what’s the full code
well it just didn't run
i changed the placement of the x = x+ 1
to under the while statement
and removed the first if
#include <iostream>
using namespace std;
int main () {
int x = 1000;
cout << "I am a four-digit number.My first digit is one-third of my last digit,and the sum of my first two digits is equal to my thrid digit.What number am I?";
while(x < 10000) {
if(x < 10000) {
x = x + 1;
if(x%10 == 3 * x%10000 && x%10000 + x%1000 == x%10) {
cout << x;
}
}
}
return 0;
}```
Seems some logical quiz, isn't it?
Yeah
Also I think it didn’t run at all because of your equation
and it should add 1 each time
until it can print out what conditions i put in the second if statement
yeah probably
i dont get how
Also I don’t think u need the first if statement
Btw, do you know ... recursion?
For ur equation to even run, u need to surround stuff in parenthesis.
For example for this part u need:
x % 10 == (3 * (x % 10000)) && …
#include <iostream>
using namespace std;
int main () {
int x = 1000;
cout << "I am a four-digit number.My first digit is one-third of my last digit,and the sum of my first two digits is equal to my thrid digit.What number am I?";
while(x < 10000) {
if(x < 10000) {
x = x + 1;
if(x%10 == 3 * x%10000 && x%10000 + x%1000 == x%10) {
cout << x;
}
}
}
return 0;
}#include <iostream>
using namespace std;
int main () {
int x = 1000;
cout << "I am a four-digit number.My first digit is one-third of my last digit,and the sum of my first two digits is equal to my thrid digit.What number am I?";
while(x < 10000)
x = x + 1;
if((x%10) == (3 * (x%10000) && (x%10000) + (x%1000) == (x%100)) { // First is meant to say if the last didgit equals 3 TIMES the fourth digit and second part is meant to say if the first digit PLUS the second digit equals the third digit)
cout << x;
}
}
}
return 0;
}```
oh yeah thats a good point
also i think i just realized
x%10
I mean most beginners don’t know that until ur code doent run at all
gives the last digit
so for my second part of the condition
it should be x%100 to give me the thrid digit i think 🤔
Lemme put stuff in parentesis first then
Idk
I'd like to share the recursion method for you, if you like.
Honestly what I would do is, open an empty project, and just do short simple tests to see how to find the digits u want
#include <cstdio>
#include <vector>
void check(int digits[], int n, int current, std::vector<int> &result) {
// if first one is 0, no 4 digit numbers
if (digits[0] == 0) return ;
// first one is 1/3 of the last one
if (digits[0] * 3 != digits[n-1]) return ;
// sum of first two equals the thrid
if (digits[0] + digits[1] != digits[2]) return ;
// who is I ?
result.push_back(current);
}
void search_result(int digits[], int n, int k, int current, std::vector<int> &result) {
// nothing to enumerate
if (k == n) {
check(digits, n, current, result);
return ;
}
// enumerate all possible digits at position k
for (int i = 0; i < 10; i++) {
digits[k] = i;
search_result(digits, n, k + 1, current * 10 + i, result);
}
}
int main() {
int digits[4];
std::vector<int> result;
search_result(digits, 4, 0, 0, result);
for (auto i : result) {
printf("%d ", i);
}
puts("");
}
Yeah I don’t think midnight knows arrays or functions yet so he def won’t know recursion
i heard an array stores mutilple number but ye i haven't gotten to those yet
Enumerate all possible four digit numbers, then check it's acceptable or not.
so far the teachers have taught us up to for loops
So you even shouldn't use std::vector collection?
I found this code online, it might help u
int main()
{
int dig;
cout << "enter a four dig num " << endl;
cin >> dig ;
int git;
git = (dig % 10);
cout << "num in once place is: " << git << endl;
int one;
one = git;
git = (dig % 100);
cout << "num in tenth place: " << (git - one) / 10 << endl;
git = (dig % 1000);
cout << "num in hundredth:" << (git - one) / 100 << endl;
git = (dig % 10000);
cout << "num in thousandth: " << (git - one) / 1000 << endl;
return 0;
}
A dynamic list-like collection for you to collect your answers together, even when you can't know the total size of them.
I'd like to use it to collect all information then output my answer in a next separate step.
yeah sounds useful
but I've only gone up four loops in the lesson so my prof probably won't let me use anything farther than that
Ummm, I can give another easy solution only concern loop.
well the one i'm working on rn is just with while loops so i can't even use for loops yet for this
Can you use function?
so what is that
I don’t think he can
His instructions just say to use a while loop
All he needs to do right now is figure out one equation and he should be done
yeah this is what i've done do far
#include <iostream>
using namespace std;
int main () {
int x = 1000;
cout << "I am a four-digit number.My first digit is one-third of my last digit,and the sum of my first two digits is equal to my thrid digit.What number am I?";
while(x < 10000)
x = x + 1;
if((x%10) == (3 * (x%10000) && (x%10000) + (x%1000) == (x%100)) { // First is meant to say if the last didgit equals 3 TIMES the fourth digit and second part is meant to say if the first digit PLUS the second digit equals the third digit)
cout << x;
}
}
}
return 0;
}```
oh i copied it twice
what i'm not understanding is how what i wrote for the math wouldn;t print
10 is supose to grab the last digit
100, theird
1000, the second
and 10000, the first
so with that in mind, what could i be doing wrong
int main()
{
int n = 7569;
int th,h,t,u; // Thousands,hundreds,tens,units
u=n%10;
t=(n/10)%10;
h=(n/100)%10;
th=n/1000;
Um let me look
so 7569%10 supose to give me 9
7569/10 gets rid of the 9, then grabs the next digit being 6
Ummm, the second demo I can give you like this:
7569/100 gets rid of both 69 and grabs 5
#include <cstdio>
#include <cstdlib>
#include <vector>
int main() {
std::vector<int> rst;
for (int current;;current += 1) {
std::div_t output;
int first, second, third, fourth;
output = div(current, 10);
fourth = output.rem;
output = div(output.quot, 10);
third = output.rem;
output = div(output.quot, 10);
second = output.rem;
output = div(output.quot, 10);
first = output.rem;
// if current can't expressed in four digits, break.
if (output.quot != 0) break;
// if first is 0, it's not the four digits number.
if (first == 0) continue;
// first one is 1/3 of the last one
if (first * 3 != fourth) continue;
// sum of first two equals the third
if (first + second != third) continue;
rst.push_back(current);
}
for (auto c: rst) {
printf("%d ", c);
}
puts("");
}
Yeah
so are u saying i should get rid of the number after it prints???
Uh no
Just so you know, the “n” value was never changed in all of those conversions
Only used for it’s 7569
Oh yeah that’s fine
Because u need to do that conversion for every single number from 1,000 to whatever
actually u know lemme run what i hvae so far
btw what is syntax
i've seen that word being used but i still don't know what it means
Like curly brackets, semi colons, parenthesis
That are required in ur code otherwise it’ll give u a fatass error
Syntax is something make the compiler know what you're talking about.
Or it won’t work like how u intend it to
Like if u forgot curly brackets after an if statement, that if statement will only run 1 instruction instead of however many u wanted it to
ohh i see
Yeah so.. ur while loop is missing something important
Yep
#include <iostream>
using namespace std;
int main () {
int x = 1000;
cout << "I am a four-digit number.My first digit is one-third of my last digit,and the sum of my first two digits is equal to my thrid digit.What number am I?";
while(x < 10000){
x = x + 1;
if((x%10) == (3 * (x%10000) && (x%10000) + (x%1000) == (x%100)) { // First is meant to say if the last didgit equals 3 TIMES the fourth digit and second part is meant to say if the first digit PLUS the second digit equals the third digit)
cout << x;
}
}
}
return 0;
}```
oh my bad i stillneed to edit
Nono before
before?
Another reason why u rlly need a good IDE lol
Yeah
Also I think you have 1 too many curly brackets at the end
Yes, what's your dev environment?
alright this is how i hve it
#include <iostream>
using namespace std;
int main () {
int x = 1000;
cout << "I am a four-digit number.My first digit is one-third of my last digit,and the sum of my first two digits is equal to my thrid digit.What number am I?";
while(x < 10000) {
x = x + 1;
if(x%10) == ((3 * (x%10000) && (x%10000) + (x%1000) == (x%100) {
cout << x;
}
}
return 0;
}```
He uses text terminal I think
yeah just terminal
Ok that’s much better
Crazy ?!
ah and now the parenthesis is the issue
man everyone keeps saying that
this is just what the teacher said to use only
at least on a mac, on windows, i think u gotta download something else
to code
Wtf I can’t think of a single reason why she would want that
Such unusual requirements.
Agreed
i think one teacher meantioned, downloading other places to work on gives u the answers
so since u need to manually learn how to code every single thing on ur own
ye i think the teacher mentioned something like that
It seems bad when you write so many expressions in one sentence. Why not use some temp variables to store the result of simple evaluations as a mid step.
ok temp variable
is the temp being declared
or is that something else
#include <iostream>
using namespace std;
int main () {```
u mean this part?
He doesn’t need to store the result though
this is just what the teacher said to write always
The only thing he’s printing is the x variable
the standard beginging
No, I mean you can use more variables in the inner loop.
So what do u think is a good way to write the equation? Because idk
inside the while loop, i need more varaibles 🤔
but don't i need just one to work with the number
could u elaborate on what u mean
For example, to evaluate the fourth digit of your number, write int l = x % 10;
Oh I see what ur saying now
Yeah that would be more clean
Then less paren would be needed in your condition.
ok that could work
So it’s easier to read
And you won't mismatch your condition expression in this style.
And just name each variable the correct place it is
Ur exactly right
alright so instead of int x = 1000
i can do
int a =
int b =
int c =
int d =
i ogtta make it equal to the mod things
so int a = x%10
Are you sure? Then a would be 100.
1000%10 would make a the last digit no?
Returns 0, which is what you want.
also i put the division sign by accident if thats what u mean
isn't return 0 to terminate the program?
I mean you need to repeat dividing then do modulo operations.
Maybe you're talking about return 0; statement in main function.
I mean the expression 1000 % 10 would returns the 0 when evaluation.
oh thats right
so i need to make it first that 1000 changes
but isn't that done with the x++
could u maybe code what u mean then?
wdym
I just suggest you use local variables to simplify your expressions to avoid bugs.
well ur saying since my x = 1000
if i just %
it would grab and print the zero
You shouldn't just do modulo but more things.
oh alright
int a = x % 10; int b = (x / 10) % 10; int c = (x / ...) ...;
Move the caocluating operations out of the if condition statements to make it clear.
right if i just made the varible each digit number, i would still be doing the same thing still
but in here the math is being done
in the varaibles
#include <iostream>
using namespace std;
int main () {
int x = 1000;
int a = x%10;
int b = (x / 10) % 10;
int c = (x / 100) % 10;
cout << "I am a four-digit number.My first digit is one-third of my last digit,and the sum of my first two digits is equal to my thrid digit.What number am I?";
while(x < 10000) {
x = x + 1;
if(x%10) == ((3 * (x%10000) && (x%10000) + (x%1000) == (x%100) {
cout << x;
}
}
return 0;
}```
i might have understood wrong
Yes, x is changing in the loop scope.
a, b, c, wouldn't changing when x is changing.
C programming language is a process oriented language, to describe some variable assignment operations with step by step statement. It's not a language to describe some mathematical INVARIANT.
so i cant make a b c equal to the math?
After one assignment statement, it assigns at that time.
It won't keep forever, if you changes something in the future.
Look at ur code where ur variables are, they will be assigned to digits of “1,000” right
Yep
Now look inside ur while loop
It’s just using the x variable, but all the Variables you made aren’t getting updated inside your loop
there is a x = x+ 1
and the if statement with the math
and then x print statement
Yeah
It will work, but you want to use your variables to simplify the equation right
So in your equation, first thing is “x%10”
yea
Variable unit
I’d Call them unit, tens, hundreds, thousands
Tbh
So , you need to move the code where u made ur variables to BEFORE if statement
And then once your variables are all set to their digits
Then do the math
Sorry I fucked up I edited it
Then do math inside if statement condition
after my while statement
if (tens == hundreds * 3 … ) {
cout << x;
}
i need to declare these varibles
Yeah
Yep
BC you want those digit variables to update every time x changes
Otherwise the digit variables will be set to 1,000 and never change, u don’t want that
#include <iostream>
using namespace std;
int main () {
int x = 1000;
cout << "I am a four-digit number.My first digit is one-third of my last digit,and the sum of my first two digits is equal to my thrid digit.What number am I?";
while(x < 10000) {
x = x + 1;
int unit = x%10;
int tens = (x / 10) % 10;
int hundreds = (x / 100) % 10;
int thousands = (x / 1000);
if(x%10) == ((3 * (x%10000) && (x%10000) + (x%1000) == (x%100) {
cout << x;
}
}
return 0;
}```
Yes exactlyyy
Now all u have to do is make your equation more readable by using your variables
so each varibles grabs a different digit
Yep
so now for the if statement part
how come
If u divide the number by 1,000
You will always get that 1,000th digit
So if it’s 5,000 divided by 1,000
It’s 5
Etc
Idk u can just test it in another terminal and see what all the math does
yeah i'll do it after i fix the if statement
so now i just gotta make the conditions
do what i want
one sec
#include <iostream>
using namespace std;
int main () {
int x = 1000;
cout << "I am a four-digit number.My first digit is one-third of my last digit,and the sum of my first two digits is equal to my thrid digit.What number am I?";
while(x < 10000) {
x = x + 1;
int unit = x%10;
int tens = (x / 10) % 10;
int hundreds = (x / 100) % 10;
int thousands = (x / 1000);
if(thousands == 3 * unit && thousands + hundreds == tens) {
cout << x;
}
}
return 0;
}```
something like this?
Match the parenthesis correctly..
thousands == 3 * unit is also wrong, pls reconsider the question description again.
thousands is the first digit, unit is the last.
i saying if the first digit equal to 3 * last digit
What it means my first digit is one-third of my last digit.
yeah if it syas that, isn't it right?
Maybe the correct statement is thousands * 3 == unit.
first digit times 3
Am I on trees or what?
= last digit
1 min ago?
I'm also confused on this situation.
damn almost 2000 messages 💀
thats what I just wondered wtf lol
the last digit is supose to be 3 times more than the 1st
Are you sure?
so if the firgit * 3, it would make the first digit three times more than the last right?
no cause well i'm always wrong but i'm not understanding how having 3 times the first digit would make it one third of the last digit
Test latex math support $x = \frac13 y$
No, You shouldn't calculate the one-third of a value.
What would happen when you want to accurate get one-third of some int value like 4.
But then it’s not supose to print
Only supose to print numbers that meet those conditions
I just tell you it's impossible to get one-third of any int.
So we play a trick on this to avoid problem.
Do both integer multiplication on the both sides to avoid the decimal problem.
So u want me to muilply 3 with thousands and unit???
I just tell you the solution to avoid decimal problem.
Alright I think I may be misunderstanding
What I’m confused about is
The decimal problem
If you do integer division, like 4 / 3, you would just get 1 but not 1.33... in C type system.
Why would there be a decimal be a problem if I declared the variables with int and not double
Because you now need a division, 'one-third' operation.
It SHould be fine though right, he doesn’t need to get the decimal value right
Rounding should get answers
In math, integer division is not complete.
But only integer numbers are being printed out anyway
So pls use multiplication to avoid this problem, but not do the stupid floating division.
That's unrelated.
We're talking about the trouble predicate, with one-third condition.
Ok so if we want the program to be accurate like u said
In that case shouldn’tI just declare the variables as double
Instead of int
I mean it wouldn't evaluate the correct division.
Because everything in the code is using integers
What happens when u run ur program rn?
But I’m trying to understand what u are saying
Good point lemme try and run it
Wait also add a space after the cout
Gimme me five minutes
I must notify you that it's a bad practice, when you code, you just check the output to trust your program rather than prove it in math.
cout << x << “ “;
wait if i have it just x;
is it only gonna printt once?
Did u run it?
loop repeats the print operations......
yeah
could u quickly explain what cout << x << " "; would mean
Nearly equivalent to
cout << x;
cout << " ";
the quotes are for space
So instead of 123
It prints
1 2 3
ohh alrigtht good to know
that means i need to do that so it does space
alright this is how it runs
But it's wrong obviously.
i need to put endl after what number am i
Reconsider your condition evaluation.
the output seems to stop before 4000
and yeah it is
well i mean it follows the condition
first two numbers
add up to the third
Are you sure?
last number is 1/3 of the first
it apears to me that the numbers do fit these conditions
its just that i didn't realize the output stopedbefore 4 thousand
what i don't understand is why because its supose to guess all the 4 digit numbers
okay i'm sorry to ask u mutilple times but can you please explain the problem again?
If you condition is proper, it's casual whatever it guess.
This condition is wrong: thousands == 3 * unit
first digit is one-third of last digit
It means the condition like thousands == 1 / 3 * unit.
hwo could that condition be wrong if the question is asking for that condition 😭
ok nvm just proceed
I have already told you the key..
You need to describe the condition like that.
the output would just print the integer
That's something you have written actually.
oh
Unrelated to the output operation.
But it's dangerous to describe a condition with division operations with int type.
first digit = 1/3 last digit
Then I suggest you to play a trick on it, with a better and equivalent condition.
so it would be 3(first digit) = 1/3 *1(last digit)
first digit = 1/3 last digit
then, 3 * first digit = 3 * 1/3 last digit
then 3 * first digit = last digit.
The equation implied.
So the condition you should use is like that 3 * thounsands == unit.
Don't you agree?
3 * 3(first digit) = 3 * 1/3 (9)(lastdigit) ??
3 * fristdigit = lastdigit
is what ur saying?
but wouldn't that make the first digit 3 times more than the last
hold on lemme read my code
#include <iostream>
using namespace std;
int main () {
int x = 1000;
cout << "I am a four-digit number.My first digit is one-third of my last digit,and the sum of my first two digits is equal to my thrid digit.What number am I?";
while(x < 10000) {
x = x + 1;
int unit = x%10;
int tens = (x / 10) % 10;
int hundreds = (x / 100) % 10;
int thousands = (x / 1000);
if(thousands == 3 * unit && thousands + hundreds == tens) {
cout << x << " ";
}
}
return 0;
}```
this is what i have
but ur saying
thousands * 3 == unit
Why you say the first digit should be 3 times more than the last?
so what i have rn is saying my first digit is equal to 3 times my last digit
the first digit for example
could be 9
right
and what im hoping is
the last digit should be a third one of 9
3
So you think it's correct, when first is 9 and last is 3?
yea
Fix it by yourself.
ok i think since i saw one third near the last digit, in my brain i kept thinking the last digit should be more
okay
#include <iostream>
using namespace std;
int main () {
int x = 1000;
cout << "I am a four-digit number.My first digit is one-third of my last digit,and the sum of my first two digits is equal to my thrid digit.What number am I?" << endl;
while(x < 10000) {
x = x + 1;
int unit = x%10;
int tens = (x / 10) % 10;
int hundreds = (x / 100) % 10;
int thousands = (x / 1000);
if(thousands * 3 == unit && thousands + hundreds == tens) {
cout << x << " ";
}
}
return 0;
}```
this time it stoped before 4000
okay but what was the other thing u were meanioning?
about the deciamls?
like this part
were u refering to the same thing?
i thought u were talking about doing math with decimals so I was confused
but yeah since it runs now and my questions are done, I'll go now
Did you really just try to ping 28,656 people?
...
oops
i thought it would just ping the people here uhh
but ye thanks everyone for the help
!solved
Thank you and let us know if you have any more questions!
This thread is now set to auto-hide after an hour of inactivity
also thank you, you made me see a mistake i had after saying it multiple times but i didn't understand so i'm sorry about that, I'm still not sure what u meant by the decimals but thank u
All good 🙂
You solve your problem correctly. About decimal, you would know it in your future lessons. Don't be worried.
what 💀 💀 💀
LMAO
I was kinda hoping we would reach the 2k mark
Maybe slowly one day
2k messages for repeatedly asking for input


