cout << string;

hi, i'm having problem displaying string:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#inlucde <cionio.h>
#include <string>
using namespace std;

int main () {
	string text;
	char znak;
	while (cin.get(znak)){
		if (znak == '\n') break;
		text += text;
	}
	cout << text;
	_getch();
	return 0;
}

this pice of code should cout string of characters including whitespaces until end of line.
however it doesn't disply anything.

how would I go to resolve this issuse ??
Last edited on
I am so confused after reading that code. First of all you never initialize text, you just add 1 to it in the loop (absolutely NO idea what this would solve) and then output it before the program is done. Also what exactly is while (cin.get(znak)) { supposed to do?

Looks to me like that loop would never go through because you never did a cin >> znak; above it. I'm also a little confused about what you're trying to do. Are you writing a program that just spams random characters in console until the end of the line then stop? Sorry if I'm a little wrong here, still fairly new myself to C++ but this doesn't look right. ;)
I just coded a little program to do what I think you're trying to do. I'm not sure if this would work as I just coded it in the forums, so I haven't actually compiled to see if it works error free. I have a feeling it won't work for a few reasons, but you can give it a shot. Let me know what happens I'm curious. ;D
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

int main () {
        bool shouldLoop = true;
	char znak;
   
        while(shouldLoop)
        {
                znack++;

                if (znack == "\n")
                {
                         shouldLoop = false;
                 }

                cout << znack;
        }
                
	cout << "We have reached a new line." << endl;
        return 0;
}
Last edited on
Line 11 isn't doing anything. It's adding an empty string to an empty string (which is later displayed empty).
this pice of code on which I'm learng wright now is copyed from my book.
refering to my book this code shall initialize string with characters including whitespaces.

the point of this code is "including whitespaces" cos cin >> ignores whitespaces.

yeah I'm confused too :/

EDIT:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#inlucde <cionio.h>
#include <string>
using namespace std;

int main () {
	string text;
	char znak;
	while (cin.get(znak)){
		if (znak == '\n') break;
		text += znak;  // here was the problem ^^
	}
	cout << text;
	_getch();
	return 0;
}


didn' see that in the book LOL sorry :)

now it work's thank you all.
Last edited on
btw is line 1: #inlucde <cionio.h> correct? Shouldn't it be #include <conio.h> ?
Topic archived. No new replies allowed.