Jump to content

C++ questions

1) So how can I input different numbers in different lines without having to press enter , in other words , is there anyway to write a code like cout<<sth<<endl; , but with cin ??? 

Ex: input should look like this : 80000  250000
                                                 37000

 

2) is there a way to know if the output of a square root will be an integer or not ? 
 

3) what code shall I use to add consecutive numbers whose total number is the input? , Ex; input is 10 m so it's supposed to add 1+2+3+4+5+6+7+8+9+10 



                                                                                                                                                                              

Link to comment
Share on other sites

Link to post
Share on other sites

1) So how can I input different numbers in different lines without having to press enter , in other words , is there anyway to write a code like cout<<sth<<endl; , but with cin ??? 

Ex: input should look like this : 80000  250000

                                                 37000

 

2) is there a way to know if the output of a square root will be an integer or not ? 

 

3) what code shall I use to add consecutive numbers whose total number is the input? , Ex; input is 10 m so it's supposed to add 1+2+3+4+5+6+7+8+9+10 

                                                                                                                                                                              

2) You can force it to be an integer by setting the type of the variable being outputted to an int, and it would then truncate the output to an integer. 

3) 

int n =10;int total = 0; while(n>0){     total+=n;      n--;}

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

1) i think you would have to use vectors, but im not positive

 

2)

if ((sqrt(x*x)) % x == 0){//it is an integer}else{//it is a double}

3) give me a minute, I will edit this and answer the question

 

edit:

int x = 10;int sum = 0; for (x = 10; x >0; sum +=x){x--;}

i know #2 could be coded a lot better. not sure how good #3 is, but it is worth a shot

about 1) i don't even know how to use vectors , so I don't think that's the way 

2) i already did that , but it won't run 

my code : #include <iostream>

 
#include <cmath>
 
using namespace std;
 
int main()
 
{
    int A;
    int x;
    x = sqrt(A);
    cin >> A;
if(sqrt(x*x) % x == 0 )
    cout << "Unknown" ;
else
    cout <<"Rectangle";
 
return 0;
}

3) sure , thank you :)

Link to comment
Share on other sites

Link to post
Share on other sites

 

2) You can force it to be an integer by setting the type of the variable being outputted to an int, and it would then truncate the output to an integer. 

3) 

int n =10;int total = 0; while(n>0){     total+=n;      n--;}

2) i don't want to force , the output helps show the answer , so i need that actually 

3)i tried to use that , but i failed to know how to fit it right into my code , and the input is not constant (10) , it depends on the user ? , thank you :)

Link to comment
Share on other sites

Link to post
Share on other sites

you are missing parenthesis after your sqrt and brackets after your if and else statements. 

brackets for if/else if are irrelevant since theres only one line of code for each 

 

And I don't think the lack of parenthesis should matter, C++ should still do the sqrt on x*x first then take the mod. 

 

 

 

2) i don't want to force , the output helps show the answer , so i need that actually 

3)i tried to use that , but i failed to know how to fit it right into my code , and the input is not constant (10) , it depends on the user ? , thank you  :)

I was just using 10 as an example. Assign n to a user input. 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

you're right, the parenthesis don't matter, but are you sure about the brackets thing? when has that been legal? is it some C++11 i don't know about lol

It's definitely legal, at least now. In both C++ and Java an if/else/for/while/do while automatically run the line immediately after them as part of the loop, regardless of brackets. 

 

Edit: just to clarify the last bit of what I said, if you do NOT include brackets it will look at the next line and automatically put brackets around in when compiling. If you do put brackets, then it will do whatever's is (or isn't) inside the brackets.

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

you are missing parenthesis after your sqrt and brackets after your if and else statements. 

post-155468-0-69433200-1414974515_thumb.

Link to comment
Share on other sites

Link to post
Share on other sites

 

you are missing parenthesis after your sqrt and brackets after your if and else statements. 

 

Did you initialize A already? If you didn't that you're going to be assigning a null value to X and taking the square root of that. 

 

Changes aren't retroactive. 

 

i.e. 

int x = 5; int a = 4;x = a;a = 3;cout << x << endl;

will print 4 not 3.  

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

take out the "x = sqrt (A)" and then run it. have the user just input x.

 

edit:

it could also be because you are doing cin << A after you are initializing x's value, which includes A, so A has no value. try flipping them around. 

 

Did you initialize A already? If you didn't that you're going to be assigning a null value to X and taking the square root of that. 

 

Changes aren't retroactive. 

 

i.e. 

int x = 5; int a = 4;x = a;a = 3;cout << x << endl;

will print 4 not 3.  

i get what you're saying , but taking it out or flipping them around doesn't change anything , it still won't run 

 

Link to comment
Share on other sites

Link to post
Share on other sites

i get what you're saying , but taking it out or flipping them around doesn't change anything , it still won't run 

 

Get rid of both altogether and set x = 100. 

int main(){     int x = 100;      if(sqrt(x*x) % x == 0 )          cout << "Unknown" <<endl;     else          cout << "Rectangle" <<endl;     return 0;}

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

i get what you're saying , but taking it out or flipping them around doesn't change anything , it still won't run 

 

here 

post-155468-0-65018000-1414975471_thumb.

Link to comment
Share on other sites

Link to post
Share on other sites

 

Get rid of both altogether and set x = 100. 

int main(){     int x = 100;      if(sqrt(x*x) % x == 0 )          cout << "Unknown" <<endl;     else          cout << "Rectangle" <<endl;     return 0;}
post-155468-0-65018000-1414975471_thumb.

 

Link to comment
Share on other sites

Link to post
Share on other sites

 

i made the sqrt function in the if statement a whole new variable. it seemed to run fine.

#include <iostream> #include <cmath> using namespace std; int main() {    int a;    cin >> a;    int x = sqrt(a);    int y = sqrt (x * x);     if( y % x == 0)         cout << "Unknown" ;    else        cout <<"Rectangle";     return 0;}

You should set x and y to be doubles, otherwise you're going to force the sqrt to be an integer. 

 

if a = 5, then x = 2, and the output would be "unknown"

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

edited

x too* 

 

I also don't see why you/OP don't just take a % sqrt(a).

#include <iostream>#include <cmath>using namespace std;int main(){    double a;    cin >> a;    if(a % (sqrt(a)) == 0.0)        cout << "Unknown" <<endl;    else        cout << "Rectangle" <<endl;    return 0;}

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

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

×