How does this code look to you?

I know I don't have to keep typing std::cout or std::cin because I didn't use the "using namespace std;" at the beginning, but does it matter? I would also like to know if my coding looks ok, and is it ok for a begginner? I have no errors or problems with it just asking from a more experienced programmer. By the way I couldn't think of nothing creative so I just tried coding in random questions, lol, so it might sound kinda stupid... Heres my Code:

#include "stdafx.h"
#include <iostream>
#include <string>

int Wait()
{
std::cin.sync();
std::cin.ignore(std::numeric_limits < std::streamsize >::max(),'\n');
return 0;
}

#define NL() std::cout<<'\n';

std::string Pyramid = " *\n * *\n * * *\n * * * *\n * * * * *\n * * * * * *\n * * * * * * *\n * * * * * * * *\n * * * * * * * * *";

int main(void)
{
std::string Line(80,'_'), Fnam, Lnam, Color; //Fnam stands for First Name, and Lnam stands for Last Name;

unsigned int Age(0), Day(0), Year(0); //All three variables contain no value 'yet'

signed int Money = sizeof(char); //char is a one byte character
signed int Blood = sizeof(char);
signed int Water = sizeof(char);
signed RESULT;

std::cout<<("::About U Information:: (AUI) Version 1.01"); //This is the header name and version
std::cout<<std::endl<<Pyramid<<std::endl<<Line; //This is the Logo for the intro

NL();
std::cout<<"Before we begin, you must be 18 years old or older to proceed...\n"; //Question
std::cout<<"Please enter your age:> "; std::cin>>Age; //User input

if (Age < 18) //if statement for user to be eligible or un-eligible to continue
{
std::cout<<"\n\tYou are un eligible to continue beyond this point\n\tdue to your age being "<<Age<<" years old.\n";
std::cout<<"\tYou will now be exited out of the prompt!\n";

Wait();
return 0;
}

std::cout<<"\n\tAge confirmed and accepted, you may continue...\n"; //Acceptance Phrase
NL();
NL();
std::cout<<"Press |Enter| to Continue\n";

Wait();
system("Cls"); //Clears Data

std::cout<<("::About U Information:: (AUI) Version 1.01");
std::cout<<std::endl<<Pyramid<<std::endl<<Line;

std::string Gender;
std::cout<<"\nAre you a (Male) or a (Female)? :> "; std::cin>>Gender;
NL();
std::cout<<"Enter only your First Name:> "; std::cin>>Fnam;
NL();
std::cout<<"Enter only your Last Name:> "; std::cin>>Lnam;
NL();
std::string Month;
std::cout<<"Enter the Name of the Month you were born on :> "; std::cin>>Month;
NL();
std::cout<<"Enter the Year you were born on :> "; std::cin>>Year;
NL();
std::cout<<"Enter the Day of the Month you were born on :> "; std::cin>>Day;
NL();
std::cout<<"Enter the name of your favorite Color:> "; std::cin>>Color;
std::cout<<"\nPress |Enter| to Continue\n";

Wait();
system("Cls");

std::cout<<("::About U Information:: (AUI) Version 1.01");
std::cout<<std::endl<<Pyramid<<std::endl<<Line;

std::cout<<"\n\tFull Name: "<<Fnam<<" "<<Lnam<<"\n";
std::cout<<"\tAge: "<<Age<<"\n";
std::cout<<"\tDate of Birthday: "<<Month<<" "<<Day<<", "<<Year<<"\n";
std::cout<<"\tGender: "<<Gender<<"\n";
std::cout<<"\tPreferred Color: "<<Color<<"\n";
std::cout<<'\t'<<Fnam<<" your first name \""<<Fnam<<"\" contains "<<Fnam.length()<<" characters,\n";
std::cout<<"\tand your last name \""<<Lnam<<"\" contains "<<Lnam.length()<<" characters.\n";
std::cout<<"\nPress |Enter| to Continue\n";

Wait();
system("Cls"); //Clears Prompt Data

std::cout<<("::About U Information:: (AUI) Version 1.01");
std::cout<<std::endl<<Pyramid<<std::endl<<Line;

std::cout<<'\n'<<Fnam<<" you have three options \'Blood\', \'Money\', & \'Water\'\n";
std::cout<<"Which of the three following choices are more important for you to be complete\n";

std::cout<<"\n\tBlood = "<<Blood<<"\n\tMoney = "<<Money<<"\n\tWater = "<<Water<<"\n\n";
std::cout<<"Enter your three importancies from which you think should\ncome first & last in life.\n";
std::cout<<"\n\tEXAMPLE: Money, Water, Blood Or Blood, Money, Water\n\n";

;std::string Name[3];
int i;

for(i=0;i<=2;i++)
{
std::cout<<"\nEnter Your Importance: ";
std::cin>>Name[i];
}

for (i=0;i<=2;i++)
{
std::cout<<'\t'<<Name[i]<<""<<std::endl;
}


Wait();
return EXIT_SUCCESS;
}

Some weird stuff you do like int main(void)

and usually i see return 0; for sucessfull exit but whatever. Do code tags btw or everyone will hate you.

I dont know if you know about this but there are great practice problems on this site if you cant come up with something: http://www.cplusplus.com/forum/articles/12974/
For future reference, use code format. Easier to read! :D
And it would be easier if you just did -

using namespace std;

Just sayin' :)
I don't know why I use int main(void) but I was watching a couple youtube videos and I read some randoms person comment that said it's good to use int main(void)... I want to understand it more... and vlad what do you mean code tags or else everyone will hate me, lol? code tags... what do you mean I should do code tags?
What is code format, can someone give me an example? I probably know, I'm just not familiarized with the names... or probably don't... idk...
[code]your code here[/code]
oh I see, the code format in cplusplus forums... lol I thought code format for c++ programming, I was confused for a second...
For more info about tags, see: "How to use tags"
http://www.cplusplus.com/articles/z13hAqkS/
BAD do it over....
int Wait()

This shouldn't need to return an int...you don't seem to use anyway.

#define NL() std::cout<<'\n';

What in the...? Don't do this, please. Especially not with a ; in the statement.

std::string Pyramid = " *\n * *\n * * *\n * * * *\n * * * * *\n * * * * * *\n * * * * * * *\n * * * * * * * *\n * * * * * * * * *";

Should be constant, if anything.

1
2
3
signed int Money = sizeof(char); //char is a one byte character
signed int Blood = sizeof(char);
signed int Water = sizeof(char);


Why sizeof(char)...? I don't understand this.

system("Cls"); //Clears Data

http://cplusplus.com/articles/j3wTURfi/

int i;

Declare these in the for loops, not outside please.

That's all I got from a quick look over it.
Well The best way i can explain why void is bad in int main is first of all you return a 0 at the end usually to signify success... so something is not right there because void signifies that you return nothing. Thats probably the easiest explanation for now. But maybe one of the better programmers here can explain better. and yeah alot of the stuff you do certainly works but is just not very standard like the #define NL() std::cout<<'\n'; and is bad practice for when you move unto bigger projects. I linked you great practice problems go slowly through all the tutorials on this site, and if you have questions start a new topic thats what we are here for. But this is a good attempt
Last edited on
closed account (zb0S216C)
iBLooDiESiN wrote:
I know I don't have to keep typing std::cout or std::cin because I didn't use the "using namespace std;" at the beginning, but does it matter? (sic)

I personally like it. I do it all the time and not once have I ever ran into a problem with it.

1
2
3
4
5
6
int Wait()
{
std::cin.sync();
std::cin.ignore(std::numeric_limits < std::streamsize >::max(),'\n');
return 0;
}
(sic)

Seems quite familiar :) Also, since it returns an integer, you should call it during the return statement of main( ). For example:

1
2
3
4
int main( )
{
    return Wait( );
}

Until you place code tags around, well, your code, it's hard to determine how good your program is.

Wazzak

just want to post my (probably) useful opinion, you can change this:

std::string Pyramid = " *\n * *\n * * *\n * * * *\n * * * * *\n * * * * * *\n * * * * * * *\n * * * * * * * *\n * * * * * * * * *";

to this:

1
2
3
4
5
6
for (int i = 1; i <= 8/*or whatever you want, it can be an user input too*/; ++i) {
	for (int j = 1; j <= i; ++j) [
		cout << "*";
	}
	cout << "endl;
} 

because the first is so wrong for the algorithm law :) (IMHO)
Thank you all for your opinions and help because I surely needed that and got the whole concept of code tags and will definitely use it. I have one minor question chipp the way you wrote ++i, is it the samething as i++? is there a reason for why one can either put i++ or ++i?
closed account (10oTURfi)
@iBLooDiESiN
Try this code
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;

int main(){
	int x, y;
	x = 10; y = ++x;
	cout << "x = 10; y = ++x; y = " << y << endl;
	x = 10; y = x++;
	cout << "x = 10; y = x++; y = " << y << endl;
	return 0;
}

It shows difference :P

And btw, i personaly think thats ugly way to code.
#define NL() std::cout<<'\n';
cant you simply use endl macro?
Or... use return 0;rather thanreturn EXIT_SUCCESS;

And for the love of god, why ; at end of #define line?

Also read this:
http://www2.research.att.com/~bs/bs_faq2.html

EDIT: and read this http://en.wikipedia.org/wiki/Computer_programming#Readability_of_source_code
Last edited on
I know I don't have to keep typing std::cout or std::cin because I didn't use the "using namespace std;" at the beginning, but does it matter?


I was looking into this myself earlier today. Came across this debate on the topic:
http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-a-bad-practice-in-c

Generally seems that writing "using namespace std::cout" and "using namespace std::cin" is OK, but simply to use use the whole of the std namespace is ill-advised because that allows hundreds (possibly thousdands I'm not sure) of functions to be used, and if you use another namespace, you can come into conflict issues.

I had personally always written "using namespace std::cout" at the beginning of all my codes but I'm still very new to this, and I think I shall change from now on.

++i pre-increments
i++ post-increments
Note that using

return EXIT_SUCCESS;

and

return EXIT_FAILURE;

is better that just blatantly returning 0 or whatever.

http://www.cplusplus.com/reference/clibrary/cstdlib/EXIT_SUCCESS/

Also see ANSI C refs.

ISO/IEC 14882 18.3

header <cstdlib> defines EXIT_SUCCESS and EXIT_FAILURE

Topic archived. No new replies allowed.