Sign in to follow this  
Followers 0
jamie79512

New to c++

20 posts in this topic

Well i tried writing a program with what little knowledge i have of c++ and was told to completely erase the program and start reading the book... anyway, as i go through the process of learning c++, i would like to post my programs here and have them critiqued... what im looking for is help avoiding any bad habits in the programming... yes i know my programs are probably terribly sloppy and there is alot about c++ that i do not understand. I also do not mind harsh or straightfoward responses as long as they are not directly mocking me.... And help or advice is much appreciated.

Here is my first program, i was reading up on relational operators and made a simple calculator program. The main complaint i see coming is all the "\n" i have spaced throughout the program, a of right now i dont know of a better way to make the program more visually appealing and this is my method... And help, advice, or "this is some of the worst code ive ever seen" replies are welcome... thanks a ton

#include <iostream>

int main()
{
using std::cout;
using std::cin;

int userOp, numOne, numTwo;

cout << "please input two numbers you wish to perfom mathematical operations on: \n";
cin >> numOne;
cin >> numTwo;
cout << "\n \n List of Operations:" << "\n (1)-multiply" << "\n (2)-divide" << "\n (3)-add" << "\n (4)-Subtract";
cout << "\n \n Which action would you like to perform?";
cin >> userOp;

if (userOp == 1)
{
cout << "\nYou have chosen multiplication...\n \n";
cout << numOne << " * " << numTwo << " = " << numOne * numTwo;
}

if (userOp == 2)
{
cout << "\nyou have chosen division...\n \n";
cout << numOne << " / " << numTwo << " = " << numOne / numTwo;
}

if (userOp == 3)
{
cout << "\nYou have chosen addition...\n \n";
cout << numOne << " + " << numTwo << " = " << numOne + numTwo;
}

if (userOp == 4)
{
cout << "\nYou have chosen subtraction...\n \n";
cout << numOne << " - " << numTwo << " = " << numOne - numTwo;
}
}

0

Share this post


Link to post
Share on other sites

The "using" statements should probably be declared globally, main() should return a value (since it's declared integer), std::endl should be used instead of "\n" in all C++ programs, as "\n" is a depreciated throwback to C, and the "if" statements should probably be a switch-case.

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

int main()
{
int userOp, numOne, numTwo;

cout << "please input two numbers you wish to perfom mathematical operations on:" << endl;

cin >> numOne;
cin >> numTwo;

cout << endl << endl << "List of Operations:" << endl << "(1)-multiply" << endl << "(2)-divide" <<
endl << "(3)-add" << endl << "(4)-Subtract" << endl << endl;
cout << "Which action would you like to perform?";

cin >> userOp;

switch (userOp)
{
case 1:
cout << endl << "You have chosen multiplication..." << endl;
cout << numOne << " * " << numTwo << " = " << numOne * numTwo;
break;

case 2:
cout << endl << "You have chosen division..." << endl;
cout << numOne << " / " << numTwo << " = " << numOne / numTwo;
break;

case 3:
cout << endl << "You have chosen addition..." << endl;
cout << numOne << " + " << numTwo << " = " << numOne + numTwo;
break;

case 4:
cout << endl << "You have chosen subtraction..." << endl;
cout << numOne << " - " << numTwo << " = " << numOne - numTwo;
break;
}
return 0;
}

0

Share this post


Link to post
Share on other sites

Hmm, i havent read up on declaring things "globally" but its an easy enough concept to understand, it seems like i had tried that with another program and it had not worked (maybe due to another error), that does make much more sense though, and clarifies a few things for me. As for using endl instead of \n, what are the advantages? I mean sure, endl was added in c++, but it seems much simpler to use \n, there are fewer characters and i dont have to declare the "using std::endl" at the beginning.

0

Share this post


Link to post
Share on other sites
Hmm, i havent read up on declaring things "globally" but its an easy enough concept to understand, it seems like i had tried that with another program and it had not worked (maybe due to another error), that does make much more sense though, and clarifies a few things for me. As for using endl instead of \n, what are the advantages? I mean sure, endl was added in c++, but it seems much simpler to use \n, there are fewer characters and i dont have to declare the "using std::endl" at the beginning.

Honestly, it doesn't really matter that much. If you really want the specifics, you could read this, but just know that in most situations (when you're using a terminal for your stdout, for example) endl and '\n' work the same. I just usually use endl for consistency's sake.

i dont have to declare the "using std::endl" at the beginning.

You do know that declaring

using namespace std;

will include cin, cout, and endl?

Edited by Spyril
0

Share this post


Link to post
Share on other sites

Yea, i did know i could use "using namespace std;", i just wasnt considering it seeing as i hadnt used it, but that does make it a little easier, i'll use endl from now on, it flows a little better i think.. Well im gonna get to work on the next program, im just using examples from a book so i can get a better understanding of what i read... thanks for the input

0

Share this post


Link to post
Share on other sites

Here is my next bit of code, this time its a simple password program, there is no real point in it except to understand the concept of "else"

//my example using else
#include <iostream>
int main()
{
using namespace std;

int passWord;
cout << endl << "Please enter the password" << endl << "Hint: Its 3 digits composed of 8, 3, and 6." << endl << endl;
cin >> passWord;

if (passWord == 386)
cout << endl << "Password accepted! Now leave...";
else
cout << endl << "Incorrect password: Access Denied";
return 0;
}

0

Share this post


Link to post
Share on other sites

You really need to read the section about scope. You shouldn't be importing a namespace inside the scope of a function and you don't need to import the entire std namespace.

0

Share this post


Link to post
Share on other sites

Do you mean i need to put the "using namespace std;" outside of int main()? is that what you mean by scope? and i know i dont have to import all of std, it was just easier. I could have just used the three i needed but it was easier to use all. And im coming up on the section about scope, i havent read that far yet.

Edited by jamie79512
0

Share this post


Link to post
Share on other sites
Do you mean i need to put the "using namespace std;" outside of int main()? is that what you mean by scope? and i know i dont have to import all of std, it was just easier. I could have just used the three i needed but it was easier to use all. And im coming up on the section about scope, i havent read that far yet.

Here's a useful tip: read the basic chapters first, then try and write some code. Stop posting every bit of code you manage to type here, that's not how you should learn. You need to learn to debug your code yourself, people won't be able to hold your hand all the time.

And yes, "using namespace std;" should be outside of main(), right after the included libraries.

0

Share this post


Link to post
Share on other sites

Well i have read the basic chapters, im working my way through the book. And i have only posted two codes... and i have received helpful input on both of them. I have learned a bit just from what ive been told, and this IS helpful when learning.. There werent really any bugs in the code, i was just looking for help at preventing bad habits in code writing. It is much easier to stop them now... all i wanted was some helpful input, but everyone seems to have an attitude about it... i guess this will be my last post... thanks to those who actually tried to help...

0

Share this post


Link to post
Share on other sites
Do you mean i need to put the "using namespace std;" outside of int main()? is that what you mean by scope? and i know i dont have to import all of std, it was just easier. I could have just used the three i needed but it was easier to use all. And im coming up on the section about scope, i havent read that far yet.

Here's a useful tip: read the basic chapters first, then try and write some code. Stop posting every bit of code you manage to type here, that's not how you should learn. You need to learn to debug your code yourself, people won't be able to hold your hand all the time.

And yes, "using namespace std;" should be outside of main(), right after the included libraries.

This is a safe place for nubies to ask questions. You can safely post here without feeling stupid or being insulted. PLEASE DO NOT FLAME THE NUBIES!!! If you do, YOU WILL BE BANNED!

The guy is just trying to better himself, be nice.

0

Share this post


Link to post
Share on other sites
Do you mean i need to put the "using namespace std;" outside of int main()? is that what you mean by scope? and i know i dont have to import all of std, it was just easier. I could have just used the three i needed but it was easier to use all. And im coming up on the section about scope, i havent read that far yet.

Here's a useful tip: read the basic chapters first, then try and write some code. Stop posting every bit of code you manage to type here, that's not how you should learn. You need to learn to debug your code yourself, people won't be able to hold your hand all the time.

And yes, "using namespace std;" should be outside of main(), right after the included libraries.

This is a safe place for nubies to ask questions. You can safely post here without feeling stupid or being insulted. PLEASE DO NOT FLAME THE NUBIES!!! If you do, YOU WILL BE BANNED!

The guy is just trying to better himself, be nice.

No, he's taking advantage of the fact that we aren't allowed to flame or yell at him in the nubie hq. He was mad at the answer I gave him in the programming section and then posted fairly similar code here. Multiple people have now said it, don't post everything you code. There isn't much to not understand about if and else statements and most of the things that he is doing wrong, any new programmer who has a decent book should know not to do. We don't mind helping people here, but he needs to help himself. It's obvious that he either has not read or learned enough to do any of the things he is trying to and needs to spend some more time learning the basics. All of these questions are things he should be learning in the first few chapters of any programming book and I'm sure most of the other members here agree with me here that we are not here to spoonfeed people.

0

Share this post


Link to post
Share on other sites
Do you mean i need to put the "using namespace std;" outside of int main()? is that what you mean by scope? and i know i dont have to import all of std, it was just easier. I could have just used the three i needed but it was easier to use all. And im coming up on the section about scope, i havent read that far yet.

Here's a useful tip: read the basic chapters first, then try and write some code. Stop posting every bit of code you manage to type here, that's not how you should learn. You need to learn to debug your code yourself, people won't be able to hold your hand all the time.

And yes, "using namespace std;" should be outside of main(), right after the included libraries.

This is a safe place for nubies to ask questions. You can safely post here without feeling stupid or being insulted. PLEASE DO NOT FLAME THE NUBIES!!! If you do, YOU WILL BE BANNED!

The guy is just trying to better himself, be nice.

No, he's taking advantage of the fact that we aren't allowed to flame or yell at him in the nubie hq. He was mad at the answer I gave him in the programming section and then posted fairly similar code here. Multiple people have now said it, don't post everything you code. There isn't much to not understand about if and else statements and most of the things that he is doing wrong, any new programmer who has a decent book should know not to do. We don't mind helping people here, but he needs to help himself. It's obvious that he either has not read or learned enough to do any of the things he is trying to and needs to spend some more time learning the basics. All of these questions are things he should be learning in the first few chapters of any programming book and I'm sure most of the other members here agree with me here that we are not here to spoonfeed people.

I couldn't say it any better.

0

Share this post


Link to post
Share on other sites
Do you mean i need to put the "using namespace std;" outside of int main()? is that what you mean by scope? and i know i dont have to import all of std, it was just easier. I could have just used the three i needed but it was easier to use all. And im coming up on the section about scope, i havent read that far yet.

Here's a useful tip: read the basic chapters first, then try and write some code. Stop posting every bit of code you manage to type here, that's not how you should learn. You need to learn to debug your code yourself, people won't be able to hold your hand all the time.

And yes, "using namespace std;" should be outside of main(), right after the included libraries.

This is a safe place for nubies to ask questions. You can safely post here without feeling stupid or being insulted. PLEASE DO NOT FLAME THE NUBIES!!! If you do, YOU WILL BE BANNED!

The guy is just trying to better himself, be nice.

The code i posted here has nothing to do with the code i posted in the other forum... I took your advice and dropped that code, started over doing only what i can handle... And so far all of the problems that i have had with these two codes havent been in my book... The endl thing hasnt come up yet, so far everything in my book has used \n,

No, he's taking advantage of the fact that we aren't allowed to flame or yell at him in the nubie hq. He was mad at the answer I gave him in the programming section and then posted fairly similar code here. Multiple people have now said it, don't post everything you code. There isn't much to not understand about if and else statements and most of the things that he is doing wrong, any new programmer who has a decent book should know not to do. We don't mind helping people here, but he needs to help himself. It's obvious that he either has not read or learned enough to do any of the things he is trying to and needs to spend some more time learning the basics. All of these questions are things he should be learning in the first few chapters of any programming book and I'm sure most of the other members here agree with me here that we are not here to spoonfeed people.

The code i posted here has nothing to do with the code i posted in the other forum... I took your advice and dropped that code, started over doing only what i can handle... And so far all of the problems that i have had with these two codes havent been in my book... The endl thing hasnt come up yet, every example in my book uses \n instead of endl, as for using "using namespace std;" outside of int main(), as with the \n, every example has it INSIDE of main(). Maybe its a shitty book, idk, i havent been skipping sections or ignoring things the book tells me to do. So far everything i have used in the code has been in examples in the book. And if your not here to help then what are you here for? what good do you do? i mean, if you dont want to help me then leave, just ignore the posts... You guys bitch about all the people coming in here asking "how do i do this?" and "how can i become good at this", but the moment someone is doing something for themselves and just asking for a little guidance to prevent bad habits you treat him like all the rest... people used to be helpful on this site...

0

Share this post


Link to post
Share on other sites

Did the book honestly put "using namespace std;" inside the main function?

Edited by Spyril
0

Share this post


Link to post
Share on other sites

yes it did, in many examples.. Ive even double checked to make sure i wasnt just reading it wrong..

0

Share this post


Link to post
Share on other sites
There werent really any bugs in the code, i was just looking for help at preventing bad habits in code writing.

Need I remind you of the code you posted in the programming section?

yes it did, in many examples.. Ive even double checked to make sure i wasnt just reading it wrong..

Then you've wasted your money on a few hundred grams of paper. If your C++ book tells you to put "using namespace std;" inside the main() function, then throw it away and get yourself a better one, because this can only go worse.

You'll find plenty of books about C++ on the website linked to in my signature.

Edited by WhatChout
0

Share this post


Link to post
Share on other sites

It didnt TELL me to put it there, its just there in every example they use. That may be because they havent covered that yet, or it may be a crappy book. Its SAMS teach yourself c++.

0

Share this post


Link to post
Share on other sites

Well its actually taught me alot so far, idk why it shows those errors in the examples, maybe they correct it later because it doesnt make much of a difference right now.

0

Share this post


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
Sign in to follow this  
Followers 0