FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Making a space in C++?





ALostSoul
I have this code

Code:

#include <iostream>
#include <conio.h>
using namespace std;
int main(void){
int age;
char yourName[10];
cout <<"Please enter your age: " <<endl;
cin >> age;
cout <<"Please enter your name with no spaces: " <<endl;
cin >> yourName;
cout <<"Hello" << yourName << "! You're" <<age << " years old!" <<endl;
cin.get();
getch();
}


and when the last cout is executed there are no spaces between the "hello" and "yourName" and the other one too...help?
ALostSoul
Come on, I need this for class tomorrow! i just need to know how to make a space!
Indi
Just put a space in there.

Code:
cout <<"Hello " << yourName << "! You're " <<age << " years old!" <<endl;
           --^--                      --^--


Incidently, you don't really need the "void" in "int main(void)", but you do need a "return 0;" before the closing "}". And 11th hour cries for help are always a bad idea.
ALostSoul
oh XD...thanks
also, i have this

Code:

#include <iostream>

int main(){
    int x = 5;
    int y = 6;
    if (x==y){
    cout << "text";
    return 0;
}
}


how come cout in undeclared?
MrBlueSky
Because 5 not is 6, so cout << "text"; is never executed:

http://en.wikibooks.org/wiki/C%2B%2B_Programming/Flow_Control#if_.28Fork_branching.29

You really need to read a book about C++
Nyizsa
ALostSoul wrote:
oh XD...thanks
also, i have this

Code:

#include <iostream>

int main(){
    int x = 5;
    int y = 6;
    if (x==y){
    cout << "text";
    return 0;
}
}


how come cout in undeclared?

You forgot
Code:
#include <conio.h>

It would be useful to rethink your time schedule on school and learning... Confused
Indi
MrBlueSky wrote:
Because 5 not is 6, so cout << "text"; is never executed:

http://en.wikibooks.org/wiki/C%2B%2B_Programming/Flow_Control#if_.28Fork_branching.29

You really need to read a book about C++

Ah, no.

It doesn't matter whether or not the branch will be executed. The variable is undeclared either way.

Nyizsa wrote:
ALostSoul wrote:
oh XD...thanks
also, i have this

Code:

#include <iostream>

int main(){
    int x = 5;
    int y = 6;
    if (x==y){
    cout << "text";
    return 0;
}
}


how come cout in undeclared?

You forgot
Code:
#include <conio.h>

It would be useful to rethink your time schedule on school and learning... Confused

And no.

<conio.h> is not a standard C++ header.

cout is undeclared because you forgot "using namespace std;"
MrBlueSky
Oops Embarassed
ALostSoul
So...


#include <iostream>
using namespace std;

int main(){
int x = 5;
int y = 6;
if (x==y){
cout << "True";
}else{
cout << "False";
return 0;
}
}

right?
Indi
ALostSoul wrote:
So...


#include <iostream>
using namespace std;

int main(){
int x = 5;
int y = 6;
if (x==y){
cout << "True";
}else{
cout << "False";
return 0;
}
}

right?

Almost - put the "return 0;" after the entire if-else block. i coloured the if block blue and the else block green to make it easier to see where they start and end:

#include <iostream>
using namespace std;

int main(){
int x = 5;
int y = 6;
if (x==y){
cout << "True";
}
else{
cout << "False";
}

return 0;
}

The reason you want the "return 0;" at the end is so that it gets executed whether the if block or the else block gets executed. If the "return 0;" is the last thing before the last "}" in main(), then you are pretty much guaranteed that it will be executed properly (ignoring things like other return statements, exceptions and jumps).
ALostSoul
Ahh, thank you Indi, you've been a great help. =] Do you have AIM by any chance?
coreymanshack
ALostSoul wrote:
Ahh, thank you Indi, you've been a great help. =] Do you have AIM by any chance?


I wish my school had computer classes, I had to teach myself. Although now I have a class at the school that teaches computer programming.
Related topics
Command and Conquer
a game online
Making a C++ text adventure
Praise the Lord...whoever they may be...
Hi !
Time Travel: Is it Possible?
Dual-Boot Windows and Ubuntu
Biggest Energy and Time Waster
Are you Making Money with Google Adds
What is the usefulness of religion?
Is time travel possible?
Obama's "space taxis" death march for NASA?
Instaling fedora over Windows 7
How to avoid fear for today and tomorrow?
Please change my domain name and server
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.