Current Events > Anyone here familiar with C++?

Topic List
Page List: 1
longsticks
04/12/17 9:32:25 AM
#1:


I'm taking an intro C++ class and the following is part of an assignment. Would this be a valid function?

Void compareValues(Circle circle1, Circle circle2)
{
if (circle.Radius1==circle.Radius2)
cout << "Both circles have the same radius./n" ;
else If (circle1.Radius>circle2.Radius)
cout << “Circle 1 has a larger radius./n” ;
else cout << “Circle 2 has a larger radius./n” ;

if (circle1.Area==circle2.Area)
cout << "Both circles have the same area./n" ;
else If (circle1.Area>circle2.Area)
cout <<Circle 1 has a larger area./n” ;
else cout << “Circle 2 has a larger area./n” ;

If (circle1.color == circle2.color)
cout << “Both circles are” << circle1.color;
else cout << “The color of Circle 1 is” << circle1.Color << “and the color of Circle 2 is “ << circle2.color
}
---
... Copied to Clipboard!
longsticks
04/12/17 9:33:33 AM
#2:


Assuming I've already defined the structure "Circle" with the members present in the function
---
... Copied to Clipboard!
Sinroth
04/12/17 9:33:43 AM
#3:


At least syntactically no, you need brackets around the conditional in your if statement.

But why don't you just try and compile it and see what it tells you?
---
I live in a big house and it's handy to have a pair of running shoes so that it doesn't take me forever to get from one area of the house to another.
... Copied to Clipboard!
DevsBro
04/12/17 9:36:04 AM
#4:


You're missing a semicolon on the last line but otherwise it looks to me like it should work.

Though for readibility reasons, always use parentheses around your if condition, and curly brackets around the if blocks and else blocks.
---
... Copied to Clipboard!
Ranting Nord
04/12/17 9:37:48 AM
#5:


What if the Radius or Area are the same? And you're missing a " and brackets
---
I'll think of a sig later.
... Copied to Clipboard!
DevsBro
04/12/17 9:37:54 AM
#6:


At least syntactically no, you need brackets around the conditional in your if statement.

It won't compile and run without them? I've never tried it in C++...
---
... Copied to Clipboard!
DevsBro
04/12/17 9:39:21 AM
#7:


And you're missing a " and brackets

Oh, yep. Didn't notice the missing quote.
---
... Copied to Clipboard!
longsticks
04/12/17 9:39:34 AM
#8:


Totally forgot about the if statement brackets, updated my original post.
Also, I have an issue with my compiler that I've been trying to fix for a week. Keeps giving me an error when I try to run the finished program
---
... Copied to Clipboard!
longsticks
04/12/17 9:40:16 AM
#9:


DevsBro posted...
You're missing a semicolon on the last line but otherwise it looks to me like it should work.

Though for readibility reasons, always use parentheses around your if condition, and curly brackets around the if blocks and else blocks.

Derp totally forgot about that too
---
... Copied to Clipboard!
IllegalAlien
04/12/17 9:40:36 AM
#10:


I haven't programmed C++ in a while but here is the fixed code for glaringly wrong syntax:


void compareValues(Circle circle1, Circle circle2)
{
if (circle1.Radius > circle2.Radius)
cout << "Circle 1 has a larger radius.\n";
else
cout << "Circle 2 has a larger radius.\n";

if (circle1.Area > circle2.Area)
cout << "Circle 1 has a larger area.\n";
else
cout << "Circle 2 has a larger area.\n";

if (circle1.color == circle2.color)
cout << "Both circles are " << circle1.color;
else
cout << "The color of Circle 1 is " << circle1.Color << " and the color of Circle 2 is " << circle2.color;
}

---
"Never argue with an idiot, they drag you down to their level, then beat you with experience."
... Copied to Clipboard!
DevsBro
04/12/17 9:41:49 AM
#11:


What does the error say?

You can try verifying whether it's your compiler by googling "compile c++ online" and copying your code in then compiling it.
---
... Copied to Clipboard!
treewojima
04/12/17 9:45:35 AM
#12:


you're free to omit brackets for if and else statements. however, the compiler will only execute the statement immediately following the conditional

if (true) cout << "it's true"
else cout << "it's false"


It's considered somewhat distasteful, especially because it can lead to code that either doesn't behave like you intended or is invalid:

if (true)
cout << "this will execute as part of the conditional";
cout << "this is NOT considered part of the conditional and will always execute";


or

if (true)
doSomething();
doSomethingElse();
else
cout << "this else statement is considered invalid because it doesn't have a matching if"


in general, I always include brackets unless it's a simple one liner
... Copied to Clipboard!
Canuklehead
04/12/17 9:45:55 AM
#13:


Some of your strings will be missing spaces.

For example:
cout << “The color of Circle 1 is” << circle1.Color << “and the color of Circle 2 is “ << circle2.color;

Will display something like "The color of Circle 1 isBlueand the color of Circle 2 is Green"
... Copied to Clipboard!
DevsBro
04/12/17 9:48:14 AM
#14:


... Copied to Clipboard!
Tony_Biggie_Pun
04/12/17 9:49:34 AM
#15:


Looks good save for syntax issues like the brackets semicolons etc. Mentioned earlier in the topic
... Copied to Clipboard!
longsticks
04/12/17 9:51:53 AM
#16:


When I try to run a program, I get this: " State Error C1010 unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source? "

I'm very new to C++, so I have no clue what that means, or why I need "stdafx.h" in my header. We've been using 'iostream' in class. If someone could help me out with this I'd really appreciate it!

Never mind, figured it out!
---
... Copied to Clipboard!
longsticks
04/12/17 10:08:37 AM
#17:


The next part is the following:
"Write a function to read values into the member variables of type Circle. (The struct variable
should be passed as an argument). The order in which the data is read is the same as that of
the items in the struct. Write a possible function call for this function."

I'm having a hard time understanding the wording of whats required, anyone have a clue?
The 'read values' part is really confusing me, it would make more sense if it was 'write values'. What exactly is the function supposed to do? Create new int values that store the member values from structure? Or just read out the member values in a cout?
---
... Copied to Clipboard!
Tony_Biggie_Pun
04/12/17 11:01:18 AM
#18:


longsticks posted...
When I try to run a program, I get this: " State Error C1010 unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source? "

I'm very new to C++, so I have no clue what that means, or why I need "stdafx.h" in my header. We've been using 'iostream' in class. If someone could help me out with this I'd really appreciate it!

Never mind, figured it out!


I'm not sure. Might have to do with the compiler your using
... Copied to Clipboard!
DevsBro
04/12/17 11:12:57 AM
#19:


So when you ask a programming question online, it's horrible etiquitte to just say "I figured it out."

You need to post what you figured out. What was causing your problem, and how you resolved it.

Not that it matters much on CE, where this topic will he gone in a couple of days but any programmer can tell you there's nothing more soul crushing than searching google for your error message, finding a post with exactly your problem and then seeing "never mind I figured it out."

No matter how embarrasing of a rookie mistake it seems like, make sure you detail the solution because even the most seasoned of developers have those derpy moments and believe it or not, there are probably fledgling programmers out there even more confused than you are, too.
---
... Copied to Clipboard!
kirbymuncher
04/12/17 11:27:18 AM
#20:


longsticks posted...
The 'read values' part is really confusing me, it would make more sense if it was 'write values'.

"read values into" means start from something empty and fill it with data (maybe from a file, maybe from user input, etc).

"read values from" means taking something that already has data in it and accessing that data to do something with it
---
THIS IS WHAT I HATE A BOUT EVREY WEBSITE!! THERES SO MUCH PEOPLE READING AND POSTING STUIPED STUFF
... Copied to Clipboard!
DarkDragon400
04/12/17 11:32:53 AM
#21:


DevsBro posted...
So when you ask a programming question online, it's horrible etiquitte to just say "I figured it out."

You need to post what you figured out. What was causing your problem, and how you resolved it.

Not that it matters much on CE, where this topic will he gone in a couple of days but any programmer can tell you there's nothing more soul crushing than searching google for your error message, finding a post with exactly your problem and then seeing "never mind I figured it out."

No matter how embarrasing of a rookie mistake it seems like, make sure you detail the solution because even the most seasoned of developers have those derpy moments and believe it or not, there are probably fledgling programmers out there even more confused than you are, too.

Relevant xkcd
https://xkcd.com/979/
---
... Copied to Clipboard!
longsticks
04/12/17 12:26:46 PM
#22:


kirbymuncher posted...
longsticks posted...
The 'read values' part is really confusing me, it would make more sense if it was 'write values'.

"read values into" means start from something empty and fill it with data (maybe from a file, maybe from user input, etc).

"read values from" means taking something that already has data in it and accessing that data to do something with it


That makes a ton more sense. Since there's no outside file involved, I'm thinking i'd pass the circle1 structure into a function which asks the user for each of the member variables and writes them into the structure? Something like:

void getValues(Circle& circle1);
{
cout << "Enter radius/n";
cin >> circle1.radius;

etc...
}
---
... Copied to Clipboard!
longsticks
04/12/17 12:28:48 PM
#23:


DevsBro posted...
So when you ask a programming question online, it's horrible etiquitte to just say "I figured it out."

You need to post what you figured out. What was causing your problem, and how you resolved it.

Not that it matters much on CE, where this topic will he gone in a couple of days but any programmer can tell you there's nothing more soul crushing than searching google for your error message, finding a post with exactly your problem and then seeing "never mind I figured it out."

No matter how embarrasing of a rookie mistake it seems like, make sure you detail the solution because even the most seasoned of developers have those derpy moments and believe it or not, there are probably fledgling programmers out there even more confused than you are, too.


True say.
Basically, during the 'create new project' section in Visual Studio Community, I had to check a box called 'Empty project'. Not sure exactly, why but I'm just glad it worked
---
... Copied to Clipboard!
Frostshock
04/12/17 12:32:15 PM
#24:


longsticks posted...
Visual Studio


I found why you're having problems learning C++.
---
Got questions about schoolwork? Want to share answers, or discuss your studies? Come to Homework Helpers!
http://www.gamefaqs.com/boards/1060-homework-helpers
... Copied to Clipboard!
Canuklehead
04/12/17 12:44:25 PM
#25:


DevsBro posted...
So when you ask a programming question online, it's horrible etiquitte to just say "I figured it out."

You need to post what you figured out. What was causing your problem, and how you resolved it.

Not that it matters much on CE, where this topic will he gone in a couple of days but any programmer can tell you there's nothing more soul crushing than searching google for your error message, finding a post with exactly your problem and then seeing "never mind I figured it out."

No matter how embarrasing of a rookie mistake it seems like, make sure you detail the solution because even the most seasoned of developers have those derpy moments and believe it or not, there are probably fledgling programmers out there even more confused than you are, too.


A very frustrating forum post has sparked an internal joke in our office now where a certain co-worker will ask a question about something and my answer is "Been there, done that!".
... Copied to Clipboard!
Topic List
Page List: 1