#New to c++ kinda on the struggle bus

38 messages · Page 1 of 1 (latest)

short reef
#

Currently writing a simple code and as of right now it is getting stuck on checking if the integer is a 2 digit integer or not.

foggy fableBOT
#

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.

drifting blaze
#

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

short reef
#

yeah I didn't know how to put the code in like other people cause Im guessing its not just copy paste

paper mesaBOT
#
How to Format Code on Discord

• 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

Example Input

```cpp
int main() {
    return 0;
}
```

Example Output
int main() {
    return 0;
} ```
short reef
#
#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;

}


drifting blaze
#

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)

short reef
#

detect if the integer is 2 digit

#

as of right now it just runs until line 15

drifting blaze
#

yes can you explain what rand() % 10 + 1; is doing?

short reef
#

picking a random integer from 1-10

drifting blaze
#

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

short reef
#

so would that while part have to go as an else if instead?

drifting blaze
#

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

short reef
#

ok thanks for the help i got it working now

#

!solved

foggy fableBOT
#

Thank you and let us know if you have any more questions!

drifting blaze
short reef
#
#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

drifting blaze
#

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++

short reef
#

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.