Hello.
I have some problems.
1.With for cycle. This is my code:

#include <iostream>
#include <fstream>
int main () {
	int i;
	int i2;
	char sentence [200];
	char repeat;
	ofstream file ("file.txt", ios::app);
	for (i=0; i<1;) {
		cout << "Enter sentence (max. 200 symbols)\n";
		cin >> sentence;
		if (!file) {
			cout << "File opening error\n";
			return 1;
		}
		file << sentence;
		for (i2=0; i2<1;) {
			cout << "Do you want to write another sentence? (y/n)\n";
			cin >> repeat;
			if (repeat == 't' || repeat == 'n')
				break;
			else
				cout << "Wrong symbol\n";
		}
		if (repeat == 'n')
			break;
	}
	file.close();
	return 0;
}

The problem is that when i input sentence program writes to file only first word. After that porgram asks me if i want to repeat, writes "wrong symbol" and then ask again if i want to repeat and asks me to write symbol (y or n). Program is asking me twice. First time i can't write y or no so it writes "Wrong symbol". What am i doing wrong?
2.How can i read from file line by line and get all sentence with spaces? Using fstream?
3.How can i check if array index is clean?

#include <iostream>
using namespace std;

int main () {
	char mas [16], *a;
	cout << "Enter word\n";
	cin >> mas;
	a = &mas [7];
	if (a == NULL)
		cout << "Word is shorter than 7 symbols\n";
	else cout << "Word is longer than 6 symbols\n";
	cin.get();
	return 0;
}

This one is not working. It's always saying me that word is shorter than 7 symbols.
Thats all for now. Sorry for my bad english i hope you understood me.
Thank you.

Edited 6 Years Ago by fire_: n/a

here is your problem

if (repeat == 't' || repeat == 'n')
     break;
else
     cout << "Wrong symbol\n";

you put 't' instead of 'y'

Ok, You have encountered the cin fail that most of us encountered as we started.

consider the following

int x;
 cin>>x;

The "problem" with cin in this case is that cin is a stream(big buffer) so it accumulates everything. you asked for an int it gave you one, but what is left on the buffer still exists, your program goes to a second cin there is still some thing on the buffer so it gives you that and your program evaluates it.

So if you want keyboard input at various stages it is good to clear the input stream between reads to make sure its not holding data. I usually stick it in before a call to cin. Narue has an exellent post about how to do this if you check the main forum area. "how to flush the input stream" or something to that effect is the name.

Hope this helps you.

Edited 6 Years Ago by Kanoisa: n/a

Comments
Thank you

here is your problem

if (repeat == 't' || repeat == 'n')
     break;
else
     cout << "Wrong symbol\n";

you put 't' instead of 'y'

Oh yes :). But i corrected it to:

if (repeat == 'y' || repeat == 'n')
     break;
else
     cout << "Wrong symbol\n";

And still same. Maybe something wrong with syntax? I'm using Microsoft Visual Studio 2005.

Ok, You have encountered the cin fail that most of us encountered as we started.

consider the following

int x;
 cin>>x;

The "problem" with cin in this case is that cin is a stream(big buffer) so it accumulates everything. you asked for an int it gave you one, but what is left on the buffer still exists, your program goes to a second cin there is still some thing on the buffer so it gives you that and your program evaluates it.

So if you want keyboard input at various stages it is good to clear the input stream between reads to make sure its not holding data. I usually stick it in before a call to cin. Narue has an exellent post about how to do this if you check the main forum area. "how to flush the input stream" or something to that effect is the name.

Hope this helps you.

Thank you. It helped and it's working fine. But now i'm not sure am i doing it correctly. I should put

cin.clear()

before using cin? and do i need to put anything else? like

cin.sync()

?

Edited 6 Years Ago by fire_: n/a

No not that im aware of basically just get the input you want from the user then clear the buffer.

One thing i will also say just now is that instead of char[] for input use the std::string type. In the long run it makes things much simpler, it can be indexed (using [] to access individual characters) and is resizable dynamically so it is always the right size, but you dont have to make it huge to try to accomodate user input wasting memory.

Edited 6 Years Ago by Kanoisa: n/a

try this

if ((repeat == 'y') || (repeat == 'n'))
     break;
else
     cout << "Wrong symbol\n";

i'm always making silly mistakes with syntax but i think it might work for you now

2.How can i read from file line by line and get all sentence with spaces? Using fstream?

And how can i read from file line by line and put text from file to dynamic array?

To get a full line you will use the get line function. getline works with a string type. such as

string str;
getline(cin,str);
//getline(istream,destination) istream can be an ifstream for reading froma  file.
//also can use getline(cin,str,';') which will read a line of input that is delimited by a ; instead of /n

The string variable type is dynamic so with that your done :)

Edited 6 Years Ago by Kanoisa: n/a

This question has already been answered. Start a new discussion instead.