new to c++

Pages: 123
Alright. I'll help you clear a few things up. I (also) am fairly new to the language. I've been toying with it for about a week, maybe less.

I am going to start at the top:

I personally prefer Code::Blocks, it is a very versatile program, you can change the Syntax Highlighting to your liking, and as you become more experienced you will notice just how many things you can customize throughout the program.

Something you may be missing out on: The steps to creating a program.
1) Open Code::Blocks
2) File --> New --> Empty File
3) Save as --> You are going to want to save it as a .cpp file, this means Code::Blocks will use C++ to compile your program.
4) Once you have completed your code, click the Build button (Blue gear on the top left of the interface)
5) After the program is built, you can click the "Run" button (arrow pointing towards the right, Located in the top left by the blue build button)

I've included a screenshot of my screen so you can locate my Build(Blue Gear) and Run(blue play) buttons, as well as the Build and Run (combination of both)
http://i54.tinypic.com/2m63hfr.png


So with that being said, you understand how to compile and run a program.
For future references; if you have a large amount of code you wish to post, and don't want to dedicate a huge post to lots of code, codepad.org can be quite useful.

In regards to questions to help you better understand program structures and data types:

Create a program that asks for a number of feet, then converts it to meters.
Be sure to use appropriate variable types! (Hint: meters and feet conversions usually aren't whole numbers (integers

Any questions, I'll be glad to answer to the best of my abilities :D
..that dus help with one of my probs


mmk i will try to make that program then post it tell me what you think when i am done
I was planning to take a break from the forums just because they're so addictive, but I must say, I'm quite impressed, norgoshi. So, I thought I'd help out a bit and give you a little quiz (as per your request). All the info for the first 5 problems is available on the first two pages. The extra credit problems... maybe not, but they're meant to broaden your view a little bit. :)

1: Can you name four fundamental data types? If so, what are they?

2: How many variables can you declare in one line?

3: DO NOT COMPILE THE FOLLOWING CODE. Given the following program:
1
2
3
4
#include <iostream>
#include <string>
int main ();
{ string heyas("Hello World!\n"); cout << heyas; return 0; }

What must you change to make this program compile?

4: What is the most usual way to end a C++ console program?

5: What purpose to comments serve in a compiled program? Un-compiled?

---

Extra Credit 1: The using keyword is not limited to just namespaces. How else can you use using to resolve scope problems?

Extra Credit 2: How can you make the code in problem 3 work by adding two identical bits of text to two different locations? HINT: the bits have :: at the end.

---

Good luck!

-Albatross
Last edited on
ok one sec i am make the program the first guy said to make so one sec should not take to long just doing the math but when i am done i will do all your mmk
need help so far i got

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

using namespace std;

int main()
{
    int feet;
    int meters;
    int result;
    
    result = feet *  0.3048m; 
}


i dont need to put meters do i? i dont think i do i no the math is right

...how do i get it to ask questions ???
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "stdafx.h"
#include <iostream>

using namespace std;

int main()
{
     // variables, double becouse it holds decimals
    int feet = 0;
    double meters = 0.3048;
    double result = 0;

    cout << "Enter the amount of 'feet' you want to convert to meters: ";
    cin >> feet;     // let the user choose feet to convert
    
    result = feet *  meters;   // do the conversion

    cout << feet << " feet equals " << result << " meters!\n\n";   // post the result

    system("pause");  // keep the program from shutting down
    return 0;
}


This is how i would do it.
Last edited on
EDIT: @Engborg: The use of system() is a very bad programming habit, and I'd strongly suggest that you get out of it. Try using two cin.ignore() statements.

@norogoshi

Hmm. ProgrammingNoob jumped the barrel a tiny little bit, but he did a good job of creating a problem that requires you to learn something in order to solve it.

To print something to the console, you use cout. Simple enough, right? Now, to read things in from the console, you use cin. Example:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

using namespace std;

int main()
{
    double input;
    cout << "Enter a decimal: ";
    cin >> input; //After you type in your decimal and hit enter, this line reads it from the console and stores it (>>) in input.
    cout << "You have entered  " << input; //A way of linking outputs in C++. Just add another << and you're good to go.
    return 0;
}


Something I'd like to warn you about: at line 11, you have one "m" too many...

Keep it up!

-Albatross
Last edited on
o and i am stuck for now so i will answer you questions..

1: int....long int short int witch is = to int.........double thats all i know....i looked and i missed a few but ya idk

2: you can put as manny as you want as long as you put commas like a, s, g, t, r, e, y,

3: int main ();<----------- take the ; of of main. wow that was a hard one did not see it till just now

4: return 0;

5:hmm umm i am not sure what your asking but i think i know umm comments well if you do not put // or /* */ then it will read it as code so thay dont do shit...just to help you and only you

EC1:no cule..

EC2:dam still dont no that one
You're doing pretty well! You might be better than I was when I was at your stage of learning... I dunno. That was some time ago, hehe. :)

1. You're just one type away from getting this one. A good one to know starts with a 'c'...

2. Correct!

3. Okay, I was a bit evil with this one. There were in fact two mistakes with the code I posted. You got one, but can you find the other?

4: Correct!

5: Actually, you got this one correct. They're just for helping whoever reads your code and nothing more.

ECs: Search around! I didn't expect you to know this at your stage in the tutorials, but they're useful to know when you're a bit further along in programming.

EDIT: I should mention this. If you're stuck on a really tough problem and you don't want to ask publicly for help (which I would encourage you to do, but hey, we all have our shy moments), then feel free to send me a private message at this link:
http://cplusplus.com/member/pmsend.cgi?recipient=Albatross
I usually check my PMs at least once a day.

-Albatross

Last edited on
1: char but i looked lol :)

3: ("Hello World!\n")<---- do you need the () and do you really need to have #include <string> why not cout <<"hello world"; thats all i can see o and this is for 3 and the EC one
i did it made the code cheek it out and yes i did use some of Engborg but i did find system pause not to be needed w/e


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

using namespace std;

int main()
{
    int feet = 0;
    double meters = 0.3048;
    double result = 0;

    cout << "put the amount of feet you want to convert into meters";
    cin >> feet;

    result = feet *  meters;

    cout << feet << " feet epuals" << result << "meters";


    return 0;
}
@Enborg
Similar to what Albatross said; don't use system("pause")
I use Ubuntu(Linux Distro), so by default it doesn't automatically exit, but for windows I don't recall the 'correct' way to make your program wait. Try googling it, or try what Albatross suggested.

@Albatross
Really? Heh, I thought I had a few simpler solutions than you, but I guess it depends on the user's knowledge base. /me thinks he should get a little farther in the tutorial before working on projects :D I personally have created many things after only completing up to Control Structures in the Cplusplus tutorial :D

@norgoshi
To output something in the terminal:
cout << "This is the string to output" << "\n";
This means ConsoleOUTput << "Text" << "\n for newline";

To input something into a variable:
cin >> variablenamehere;
ConsoleINput

NOTE!: Input is arrows to the right, and output is arrows to the left! Don't ever forget that! :)


@Albatross/Norgoshi
1) integer (int) //whole numbers; boolean (bool) //true or false value; double (double) //decimal value; string (string) //line of text; character (char) //single number/character;

2) Correct, you can define many variables seperated by commas (keep in mind they are all the same variable type as the first!)

3) int main() shouldn't have a colon, and you should have using namespace std;

4) Yes, return 0;

5) comments are for anyone that may be reading your code, refer to emborg's code for first hand examples!
Indeed, what I posted was a not-very-efficient way of printing
Hello World!
to the screen, and it would have been simpler in this case to bypass the variable nonsense. However, that's not a mistake, per-se. :)

I suppose I'll give a major hint for 3, as it's a mistake I used to make. Take note of this, because when you get into more advanced programming it could be a pain. Aren't cout and string in a namespace?

EDIT: Dangit. :)

-Albatross
Last edited on
lol noob me i see it nor c in>>>>c out<<<<<< in and out lol got it
OMFG ninja i did not even look to see if you put namespace....wow....i am noob...ok wow awsome hmmmm


Well; look at the bright side! You're learning!
I wish I'd this kindof support when I had questions, I suppose I'll make my own topic and hope people bless me as well...

/me goes does that
mmk well it looks like i can move on to part 3 cus it really seems like i have this down....ok when i need more Quizzes and help i will post and i am sure that will not be long lol :)
have a few questions


1:#define so by useing this i could say that tacos /n and from there on out tacos would be /n right?

2:octal and hexadecimal and decimal so this is the 3 types of numbers Integer Numerals can read or the 3 thay can be used in or some one plz make it not so complex if you would be so kind :)

thats it i get the rest just want to bee 100%sure :)
1) #define is to define a constant throughout the program
I wouldn't recommend it, but theoretically, yes, you can do it like this:
#define tacos '\n' <-- Right here I think you meant to put \n (newline) instead of /n

This makes the following statement:
cout << "I have 10 fingers. " << tacos; <--- here, tacos replaces 'endl;' and ' "\n"; '
As far as hex and octal numbers, I wouldn't spend too much time on that at this particular point in time. I believe the authors of the tutorial just wanted to make you aware of the existence in the event of you coming across one of them.
Pages: 123