Small question

When i do this
printf("Blablabla")
printf("blablabla")

Instead of printing the second line in the second line it prints both in the same line how to make it print in second line and how to give spaces in c++
'\n' is the new line character. And if you want to print a space, just print a space

1
2
3
4
5
printf("line 1\nline 2"); // note you can put spaces in just fine

// or if this is C++

cout << "line 1\nline 2";
Last edited on
printf("bla\n");

But that is C, not C++, so it doesn't answer the question (although I have the feeling it does clear your doubt).

In C++, you would:

1
2
cout << "blablabla" << endl;
cout << "more blabla";


BTW, please use code tags as they make code look pretty and readable.
1
2
3
std::cout <<"blblabla" <<std::endl;

I get undeclared identifier error with and without std before endl
You have to include <iostream>.
I did
Eh, are you sure you aren't using a C compiler?
I use visual c++
Might want to post the complete source you're trying to compile then.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{

std::string word;
std::cin >> word;
std::string last_2 = word.substr(word.size() - 2);
if(last_2 == "is" )
std::cout << "You are dead";
std::cout << "Yay!!";
else if(last_2 == "wer")
printf("you are alive");
else std::cout << "Nothing";
std::cin.get();
std::cin.get();
  return 0;
}


another thing after one function is called and i press enter program exits

another thing
I don't see any #include <iostream> in there, nor are you including <string>.

another thing after one function is called and i press enter program exits

What function and what do you expect to happen anyway?

Edit: the else if is misplaced too. You need to put braces around a block.
Last edited on
i just didnt paste the inludes they are available in my code, And now i fixed that error, But exit error still exists, Cant figure out a way, See what i need is when he types is , It says u are dead and yay press enter again the program exits, But what if he wants to type another is or wer in the same console without restarting, And after he types wer the previous text should be erased, Thanks , hope u understand
You've left off the std::endl. Havent you realised that's what writes the end on line character?
closed account (zb0S216C)
Athar wrote:
nor are you including <string>.
<iostream> includes <string> in Visual C++ Express.

gauthamnekk wrote:
But what if he wants to type another is or wer in the same console without restarting

Consider a while loop. You can write your loop like this:

1
2
3
4
5
6
7
8
9
10
11
12
int main( )
{
    std::string word;
    bool exit( false );
 
    while( !exit )
    {
        // Your code here...
    }

    return 0;
}


Wazzak
Last edited on
<iostream> includes <string> in Visual C++ Express.


But it doesn't have to. That might change in a future version of VC++, for example. And it might not be the case if you try to compile on another compiler.

If you're using string, it's best to #include <string> specifically and not assume it's already included via another header.
Last edited on
closed account (zb0S216C)
Disch, could it be possible to add conditional compilation directives to change which headers are included during compilation/linking? For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef __HEADER_H__
#define __HEADER_H__

#ifdef __MS_COMPILER__
#include <iostream>
#endif

#ifdef __GNU_COMPILER__
#include <iostream>
#include <string>
#endif

// And so forth...

#endif 


Wazzak
Last edited on
I suppose so... but why?

Why not just #include <string> if you're using string?

KISS
closed account (zb0S216C)
Disch wrote:
I suppose so... but why?.

Just a thought.

Disch wrote:
Why not just #include <string> if you're using string?

Could do.

Disch wrote:
KISS

If you're a lad, I'm afraid to tell you that I don't swing that way.

Wazzak
Last edited on
=P

I can't tell if you're being sarcastic or not, but KISS is an acronym that's semi-popular in programming circles meaning "Keep It Simple, Stupid".
closed account (zb0S216C)
Disch wrote:
I can't tell if you're being sarcastic or not

My profile gives you an idea of how sarcastic I can get :)

Disch wrote:
but KISS is an acronym that's semi-popular in programming circles

Nope. I've never heard of that.

Disch wrote:
"Keep It Simple, Stupid"

Lol. I won't be asking you for acronyms anytime soon; I don't know what they could mean, unless you tell me, of course :)P

Topic archived. No new replies allowed.