Write text to a file

Hello!

I´m kind of a noob when it comes to c++. Right now I have a project where I'm going to write text to a file; socical security number (text?) and name.

Something is wrong, because I keep getting a "Link Error"(fatal error LNK1120: 1 unresolved externals), even when I'm trying to debug a program that seems to work ok for others. I know that I have missed something, but I can't figure out what it is and I've never write to a text file before. My code look like this:

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;


int main()
{
char name, teleNr svar;
ofstream out;

out.open("DB.txt");

if (!out.is_open())
{
return false;
}

do
{
name = 0;
teleNr = 0;

cout << "Skriv in ditt namn: ";
cin >> namn;

cout << "Skriv in ditt personnummer: ";
cin >> personNr;

utfil << namn << " " << personNr << "\n";

cout << "Vill du mata in ett nytt namn? ";
cin >> svar;

} while (svar == 'J' || svar == 'j');

utfil.close();
return 0;
}


I'll be happy for some help
A linking error occurs when you don't have that library installed.

Your actual code must be quite different because there are numerous compilation errors here which would mean that it will fail before getting to the linking phase.

Here are some of the issues:
1. There should be a comma between teleNr and svar
2. namn is undefined
3. PersonNr is undefined
4. utfil is undefined.

I suppose that you could have those things declared in your std SDK if you have a really strange compiler. In that case you may get a linking error.

Is it possible that you've been editing the iostream, iomanip or fstream headers? If you've declared anything there, you'll have problems.
Last edited on
Thanks, man!

How do I install the library?

The other issues is mainly because I began to translate swedish to english, and obviously forgot to implement it to the end of the file :)
Hi,

just try to create a new project and copy it then paste it on the new project , this could work.
Thank you, Eyad!

Now the program can start, but it closes after the first "cin"...

Why is that? My code now look like this:

int main()
{
char namn, teleNr, svar;
ofstream utfil;

utfil.open("DB.txt");

if (!utfil.is_open())
{
return false;
}

do
{
namn = 0;
teleNr = 0;

cout << "Skriv in ditt namn: ";
cin >> namn;

cout << "Skriv in ditt telefonnummer: ";
cin >> teleNr;

utfil << namn << " " << teleNr << "\n";

cout << "Vill du mata in ett nytt namn? ";
cin >> svar;

} while (svar == 'J' || svar == 'j');

utfil.close();
return 0;
}
The problem now is that namn, teleNr and svar are all chars.

This means that if you write several characters for the name (namn) then it'll stick the first character in namn, the second character in teleNr and the third character in svar.

Solution
Make namn a std::string and teleNr an int.

This works:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
	std::string namn;
	int teleNr;
	char svar;

	ofstream utfil;

	utfil.open("DB.txt");

	if (!utfil.is_open())
	{
		return false;
	}

	do 
	{
		namn.empty();
		teleNr = 0;

		cout << "Skriv in ditt namn: ";
		cin >> namn;

		cout << "Skriv in ditt telefonnummer: ";
		cin >> teleNr;

		utfil << namn << " " << teleNr << "\n";

		cout << "Vill du mata in ett nytt namn? ";
		cin >> svar;

	} while (svar == 'J' || svar == 'j');

	utfil.close();
	return 0;
}
Topic archived. No new replies allowed.