#New to c++ kinda on the struggle bus
38 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.
well okay few things you use printf, in c++ you generally should use std::cout
i personally dont like do while and you can usually achieve the same thing with while
other thing is please indent your code properly since you posted a screenshot its much easier to read that way or post the code here
yeah I didn't know how to put the code in like other people cause Im guessing its not just copy paste
• type three "backticks" (not quotes/apostrophes, on QWERTY layout, left of 1-key)
• on the same line, type the file extension for that language (c or cpp)
• enter your code on a new line and put another three backticks at the end
```cpp
int main() {
return 0;
}
```
int main() {
return 0;
} ```
#include<iostream>
using namespace std;
int
main ()
{
int num, num2;
num = rand () % 10 + 1;
num2 = 4 * num - 20;
do
{
if (num < -9 || num>9)
printf (" You entered: %d", num);
}
while (-9 <= num <= 9);
printf (" error ");
do
{
if (num2 < -9 || num2 > 9)
printf (" You entered: %d", num2);
}
while (-9 <= num2 <= 9);
printf (" error ");
return 0;
}
yes amazing
now the code is very hard to read because you didnt indent and everything has a slightly different style
#include <iostream>
using namespace std;
int main()
{
int num, num2;
num = rand() % 10 + 1;
num2 = 4 * num - 20;
do
{
if (num < -9 || num > 9)
printf(" You entered: %d", num);
}
while (-9 <= num <= 9);
printf(" error ");
do
{
if (num2 < -9 || num2 > 9)
printf(" You entered: %d", num2);
}
while (-9 <= num2 <= 9);
printf(" error ");
return 0;
}
ran my autoformatter over the code and this is what i get
now what do you actually want to do ?
detect if the integer is larger than 9 right (or less than -9)
yes can you explain what rand() % 10 + 1; is doing?
picking a random integer from 1-10
good, now imagine it generates 2
you enter the first while loop
and here we already have a problem
-9 <= num <= 9 is not a valid c++ syntax
you would need to do -9 <= num && num <= 9 to get the same behaviour
but if that number was 2 then the while loop is true
you have a while loop that does stuff infinitely because you dont change num in the while loop
and the comparison is true
so would that while part have to go as an else if instead?
if else would make more sense in this entire code
i recommend you convert this all to if else and remove printf and instead use cout
Thank you and let us know if you have any more questions!
can you post it again though?
#include <iostream>
using namespace std;
int main()
{
int num, num2;
num = rand() % 10 + 1;
num2 = 4 * num - 20;
if (num < -9 || num > 9)
printf(" You entered: %d", num);
else if (-9 <= num && num <= 9)
printf(" You entered: %d", num);
if (num2 < -9 || num2 > 9)
printf(" You entered: %d", num2);
else if(-9 <= num2 && num2 <= 9)
printf("You entered: %d", num2);
return 0;
}
ik it isnt proper but i now have it differentiating them and just have to right some extra code for it to function differently when given an integer that is one digit
well for code readability you should move your ifs a bit more to the left so that they match with num and num2, but you always print out printf("You entered: %d", num2); so whatever happens it will always print that out
so you could remove your ifs and still have the same code
#include <iostream>
using namespace std;
int main()
{
int num, num2;
num = rand() % 10 + 1;
num2 = 4 * num - 20;
cout << "You entered: " << num << endl;
cout << "You entered: " << num2 << endl;
return 0;
}
amd you shouldnt use printf in c++
I understand as of right now the ifs aren't required but I'm going to make it so one digits display as text. This was just the setup of that.