Need Help With Tutorial...

So, I went to the tutorials offered via cplusplus and am having trouble getting reality to meet expectation.

Their code(www.cplusplus.com/doc/tutorial/ntcs/):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// strings and NTCS:
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  char question1[] = "What is your name? ";
  string question2 = "Where do you live? ";
  char answer1 [80];
  string answer2;
  cout << question1;
  cin >> answer1;
  cout << question2;
  cin >> answer2;
  cout << "Hello, " << answer1;
  cout << " from " << answer2 << "!\n";
  return 0;



What is your name? Homer
Where do you live? Ancient Greece
Hello, Homer from Ancient Greece!


My code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
using namespace std;

int main()
{
    char question1[] = "What is your name? ";
    string question2 = "Where do you live? ";
    char answer1 [80];
    string answer2;
    cout << question1;
    cin >> answer1;
    cout << question2;
    cin >> answer2;
    cout << "Hello, " << answer1;
    cout << " from " << answer2 << "!\n";
    system("PAUSE");
    return 0;
}



What is your name? Homer
Where do you live? Ancient Greece
Hello, Homer from Ancient!
The example is wrong. cin >> answer2; will only read one word.
closed account (j3Rz8vqX)
Wow, good catch on the tutorial example.

Beginner post about something similar:
http://www.cplusplus.com/forum/beginner/122539/#msg667824

A possible alternate solution would be:
http://pastie.org/8689954#12,14,18

Getline() References:
std::string getline(): http://www.cplusplus.com/reference/string/string/getline/
istream getline(): http://www.cplusplus.com/reference/istream/istream/getline/

Have fun.

[Edit]:
I feel that "cin" is acceptable for acquiring primitive data, however it's drawback is possibly safety.
Things to consider when working with "cin":
(whitespaces, tabs, new-line...)
Last edited on
Thanks to your help,
A possible alternate solution would be:
http://pastie.org/8689954#12,14,18
I was able to figure out why,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main ()
{
    string word;
    char definition;
    cout<<"Please give word"<<endl;
    cin>>word;
    cout<<"And it's definition?"<<endl;
    cin>>definition;
	ofstream myfile ("dic.txt");
	if (myfile.is_open())
	{
		myfile<<word<<'\t'<<definition;
		myfile.close();
	}
	else cout<<"Unable to open file";
	system ("PAUSE");
	return 0;
}
wasn't sending the entire string, "definition," to the file.

I have since rewritten it,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main ()
{
	string word, definition;
        ofstream myfile ("dic3.txt");
	if (myfile.is_open())
	{
		cout<<"Please enter word: ";
		getline(cin,word);
		cout<<"Please enter its definition: ";
		getline(cin,definition);
		myfile<<word<<'\t'<<definition;
                myfile.close();
	}
	else cout<<"Unable to open file";
	system ("PAUSE");
	return 0;
}
But have ran into a new problem, every time I run the program it erases all of the previous information.

For example, file dic.txt might read:
Book '\t' A collection of pages
Until I run the program again, then it's replaced with the new data. How can I keep this from happening, so with time more entries are saved? And how might I make it alphabetize the entries?

Also, why/how is "cin" lacking in security?
Last edited on
When opening the file, specify that you are appending to the file, like so:
std::ofstream myfile ("dic3.txt", std::ios::app);

See http://www.cplusplus.com/reference/fstream/filebuf/open/
Thanks! I've finally got it to work the way I want!!! Thank you to every one!

Out of curiosity, to make it easier to scan the file by hand, Is there a way to make everything alphabetical?
Topic archived. No new replies allowed.