Forums

Topic: Coding Discussion

Posts 1 to 10 of 10

RR529

I've decided to try and play around with coding to see if I have a knack for it (I'm almost 30, but you're never too old to learn something new, as the saying goes), and am goofing around in C++.

I think I've gotten the hang of producing simple text prompts (based on the "Hello, World" tutorial), but am trying to get it to "solve" a math problem and am obviously doing something wrong. What I currently have is:

int a=10;
int b=50;
int c=a+b;
cout<<c<<

Of course I'm trying to get it to solve 10+50, but there's some sort of error in the code that I'm not able to get my head around and can't get it to run (I've tried a few slight variations to no avail).

I know we've had threads like this before, but the most recent prominent one was from around 2015 or so!

Currently Playing:
Switch - Blade Strangers
PS4 - Kingdom Hearts III, Tetris Effect (VR)

D-Star92

@RR529 I've looked at the code you've provided. It looks mostly fine to me, but there's no "endl;" after the cout statement. (FYI - endl stands for "end line.") Try putting that in, as well as "return 0;" at the very end of the main function if you haven't already - that should fix it.

"Give yourself the gift of being joyfully you."

Favorite games: Super Mario 3D World, Animal Crossing: New Horizons
Playing: Hollow Knight

Ask if you want to be Switch friends with me, but I'd like to know you first. Thanks! ❤️

My Nintendo: D-Star92

Hemera

RR529 wrote:

cout<<c<<

I'm just going to assume, that you have either iostream.h included or iostream with a defined std namspace.
Your issue beside the missing ";" in your last line is the "<<" after the c. Either get rid of them or as explained above me add an endl. While endl is not neccessary in this example it is good form to use it anyway as it closes your output stream.

Hemera

Cynas

@MarioLover92 Neither of those should be necessary. C++ will automatically return 0 in the main function even if you don't manually put it. It can be good practice to include it but it won't cause any issues. Endl is similar to using backslash n, it inserts a new line. The main difference is endl flushes the stream in addition to inserting a new line.

As Hemera said, make sure you include iostream and don't forget the semicolon at the end of your cout statement. You could just do:
cout << c;
Or you could do something like:
cout << "variables a and b add to get" << c;
(Just to practice including both variables and text in your cout)

For C++ I'd really recommend looking into pointers to make sure you can wrap your head around them, and maybe try making a few simple classes with getters and setters and create instances of those classes in your main class to get used to object oriented programming.

[Edited by Cynas]

Cynas

Switch Friend Code: SW-5466-6715-6498

D-Star92

@Cynas Oh right, you could forego the endl and the return 0. I just tested that out. For the first case, it just won't make a new line. I was just trained to have both of those when I was taking C++ courses, lol. And yes, including iostream and namespace std are crucial for writing C++ programs.

I've learned about pointers and trees too. I do remember hearing that you should be super careful while making pointers. Don't they cause stuff like memory leaks if they're used improperly?

"Give yourself the gift of being joyfully you."

Favorite games: Super Mario 3D World, Animal Crossing: New Horizons
Playing: Hollow Knight

Ask if you want to be Switch friends with me, but I'd like to know you first. Thanks! ❤️

My Nintendo: D-Star92

Cynas

@MarioLover92 Yeah pointers can cause memory leaks. If you something like the following:

int *numbers = new numbers[100];

It allocates space in memory for 100 integers. If you don't explicitly return this allocated space using 'delete', it could cause a memory leak. You would want to use:

delete [] numbers;

I think generally if you ever initialize a pointer using the 'new' operator, you want to have a 'delete' later on for it.

@RR529 You should also try some simple conditional and loop statements (like 'for' and 'if/else' statements). They're some of the most common things you'll use in functions.

Cynas

Switch Friend Code: SW-5466-6715-6498

D-Star92

@Cynas Okay - I'll definitely keep that in mind. I'm thinking it would be good to delete pointers at the end of programs. I have done some experimenting by making a simple text adventure game out of a C++ program. Would be pretty cool if I could make a platformer on Visual Studio, lol

"Give yourself the gift of being joyfully you."

Favorite games: Super Mario 3D World, Animal Crossing: New Horizons
Playing: Hollow Knight

Ask if you want to be Switch friends with me, but I'd like to know you first. Thanks! ❤️

My Nintendo: D-Star92

RR529

I really think I was just getting really ahead of myself. Went back to just displaying text and learned how to break it up into multiple lines today.

Currently Playing:
Switch - Blade Strangers
PS4 - Kingdom Hearts III, Tetris Effect (VR)

RR529

Been learning bit by bit for the past few days and have made a working math quiz.

int main()
{
/* I am adding
a multiline comment */
int a, b, c, d;
cout << "Time for a math quiz!" << endl;
cout << "2 + 2 = ";
cin >> b;
if (b == 4) {cout << "Correct" << endl;
cout << "5 + 5 = ";
cin >> c;
if (c == 10){cout << "Correct" << endl;
cout << "5 - 4 =";
cin >> d;
if (d == 1){cout << "Correct!" << endl;
cout << "You are a math whiz!";}
else {cout << "Sorry!" << endl;
cout << "Almost there!";}}
else {cout << "Sorry!" << endl;
cout << "Halfway There!";}}
else {cout << "Sorry!" << endl;
cout << "Please Try Again.";}
return 0;
}

Currently Playing:
Switch - Blade Strangers
PS4 - Kingdom Hearts III, Tetris Effect (VR)

Dezzy

@RR529

Well done. I remember how fun it was learning this stuff, when you first write a program that actually does something useful or interesting. My only suggestion is that you don't need to divide a message up into several statements.

cout<<"Sorry!"<<endl;
cout<<"Almost there!";

Can be shortened to the single statement:

cout<<"Sorry!<<endl<<"Almost there!";

This gives the exact same output as your original. It's generally best (for all kinds of reasons) to use fewer statements if possible. It means, for example, you can avoid using the curly brackets with your "if/else" statement.

else{
cout<<"Sorry!"<<endl;
cout<<"Almost there!";
}

can be shortened to just the single line:

else cout<<"Sorry!"<<endl<<"Almost there!";

With small simple programs, these differences will maybe just sound like people being pedantic for the sake of it, but when you start to write bigger more complex things, you'll get to a point where it's genuinely hard to understand your own code if you leave it for a week or so (or if you need to show it to someone else). At that point, it's incredibly beneficial to have everything be as short and simple as possible.

[Edited by Dezzy]

It's dangerous to go alone! Stay at home.

  • Page 1 of 1

This topic has been archived, no further posts can be added.