Wrong outputting the input file.

I'm trying to get my input working, but it doesn't seem to output correct.
Any help ?

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
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <fstream>
const char CDfv[] = "U2.txt";
const char CRfv[] = "U2rez.txt";
const int CMax = 26;
const int Cn = 125;
using namespace std;

struct TData
{
    int met, men, dien;
};
struct TZmogus
{
    TData gim, mir;
    char VP[CMax];
    int amzius;
};

int n;
TZmogus A[Cn];

void Skaityti()
{
    ifstream fd (CDfv);
    fd >> n;
    fd.ignore();
    for(int i = 0; i < n; i++)
    {
        fd.get(A[i].VP, sizeof CMax);
        fd >> A[i].gim.met >> A[i].gim.men >> A[i].gim.dien;
        fd >> A[i].mir.met >> A[i].mir.men >> A[i].mir.dien;
        fd.ignore();
    }
    fd.close();
}
void Spausdinti()
{
    ofstream fr (CRfv);
    fr << n;
    for(int i = 0; i < n; i++)
    {
        fr << A[i].VP;
        fr << A[i].gim.met << A[i].gim.men << A[i].gim.dien;
        fr << A[i].mir.met << A[i].mir.men << A[i].mir.dien;
    }
    fr.close();
}
int main()
{
    Skaityti();
    Spausdinti();
}


Input:
1
2
3
4
5
6
7
8
9
8
Albertas Einšteinas 1879 03 14 1955 04 18
Balys Sruoga 1896 02 02 1947 10 16
Antanas Vienuolis 1882 04 07 1957 08 17
Ernestas Rezerfordas 1871 08 30 1937 10 17
Nilsas Boras 1885 10 07 1962 11 18
Nežiniukas Pirmasis 8 05 24 8 05 25
Nežiniukas Antrasis 888 05 25 888 05 25
Nežiniukas Trečiasis 1 01 01 125 01 01


Output:
 
8Alb000000000000000000000000000000000000000000000000
There are a couple of things wrong with the following line:
fd.get(A[i].VP, sizeof CMax);

First I think you need to read up on sizeof, you'll probably be surprised at the value of that call.

Next since your input file doesn't appear to be using fixed length strings get() is probably not the method you should be using. And unless your strings are being separated by something other than a space character (ie a tab character) retrieving the "whole" name may be problematic. Perhaps you should try retrieving first name and last name separately.

Thank you for reply!

I changed my name char to two different strings.

Would there be a way to have a full name with a space between words in a char that can be outputted ?

What if have to my output with spaces like this:
1
2
3
4
5
6
7
8
9
8
Albertas Einšteinas                    1879 03 14   1955 04 18
Balys Sruoga                              1896 02 02   1947 10 16
Antanas Vienuolis                      1882 04 07   1957 08 17
Ernestas Rezerfordas                1871 08 30   1937 10 17
Nilsas Boras                               1885 10 07   1962 11 18
Nežiniukas Pirmasis                         8 05 24        8 05 25
Nežiniukas Antrasis                      888 05 25   888 05 25
Nežiniukas Trečiasis                         1 01 01   125 01 01 


Last edited on
Yes output the first string then a space character then the second string.

Why are you using C-strings instead of C++ strings?
Code seems to mess up with spaces. Here's imgur: http://i.imgur.com/dp5zdR6.png

How can I make spaces between the words and number like that ?

Why are you using C-strings instead of C++ strings?

What do you mean by that ? Sorry, I'm still a newbie.
Last edited on

What do you mean by that ? Sorry, I'm still a newbie.

I mean you're writing a C++ program so you really should be using C++ strings not old fashioned C-style character strings.

Code seems to mess up with spaces.

Without seeing your current code I can only guess. But you may want to investigate some of the manipulators available from the <iomanip> header.

The main reason why I wanted to use char with fixed size was so I could then use same ammount of spaces between word and number despite the lenght of the name. I don't know how to make it with string.

1
2
3
4
5
6
7
for(int i = 0; i < n; i++)
    {
        fr << A[i].V << " " <<  A[i].P << setw(10);
        fr << A[i].gim.met << " " << A[i].gim.men << " " << A[i].gim.dien << " ";
        fr << A[i].mir.met << " " << A[i].mir.men << " " << A[i].mir.dien << " ";
        fr << endl;
    }

Albertas Einšteinas      1879 3 14 1955 4 18 
Balys Sruoga      1896 2 2 1947 10 16 
Antanas Vienuolis      1882 4 7 1957 8 17 
Ernestas Rezerfordas      1871 8 30 1937 10 17 
Nilsas Boras      1885 10 7 1962 11 18 
Nežiniukas Pirmasis         8 5 24 8 5 25 
Nežiniukas Antrasis       888 5 25 888 5 25 
Nežiniukas Trečiasis         1 1 1 125 1 1 


Last edited on
The main reason why I wanted to use char with fixed size was so I could then use same ammount of spaces between word and number despite the lenght of the name.

But as you can see using a C-string doesn't help and is actually much more error prone.

To do what you want you'll probably want to combine the first and last names into a "temporary" string then print that string using one of the manipulators found in the <iomanip> include file so that all the strings print the same number of spaces.

http://www.cplusplus.com/reference/iomanip/

I cannot find any manipulator in the reference that could do that, though.
So I take it you never clicked on any of the links under "Parametric manipulators" on that page? Perhaps you should look at setw()?

Doesn't setw() only create raw spaces ? That would not really work since the size of names is different. What I need output to look like is: http://i.imgur.com/dp5zdR6.png.

I believe setw() would not help.
Your situation is exactly what setw() is made for, actually. Go try it.
Or better yet go and actually read the documentation: http://www.cplusplus.com/reference/iomanip/setw/

1
2
3
4
5
6
7
for(int i = 0; i < n; i++)
    {
        fr << A[i].V << " " <<  A[i].P << setw(10);
        fr << A[i].gim.met << " " << A[i].gim.men << " " << A[i].gim.dien << " ";
        fr << A[i].mir.met << " " << A[i].mir.men << " " << A[i].mir.dien << " ";
        fr << endl;
    }

Albertas Einšteinas      1879 3 14 1955 4 18 
Balys Sruoga      1896 2 2 1947 10 16 
Antanas Vienuolis      1882 4 7 1957 8 17 
Ernestas Rezerfordas      1871 8 30 1937 10 17 
Nilsas Boras      1885 10 7 1962 11 18 
Nežiniukas Pirmasis         8 5 24 8 5 25 
Nežiniukas Antrasis       888 5 25 888 5 25 
Nežiniukas Trečiasis         1 1 1 125 1 1 



I tried that earlier. Can't really figure out how to make the space consistent despite the size of names.
So what exactly is A[i].V and A[i].P?

It appears that you will either need two setw() calls, which should precede the variable, one for the first string and then another for the second string or first combine the two strings then use a single setw() call.



Last edited on
So what exactly is A[i].V and A[i].P?

First name and Last name.

It appears that you will either need two setw() calls, which should precede the variable, one for the first string and then another for the second string or first combine the two strings then use a single setw() call.

Having a space between names would not make any sense, I think that.

The string (or two separate strings) still needs to have a fixed size or something like that since if I want to have same space between a word and numbers, like in the example:

http://i.imgur.com/dp5zdR6.png.
Why not something like:
1
2
3
4
5
6
while(something to print)
{
   string temp = A[i].V + " " + A[i].P;  // All of these variables are strings.
   cout << setw(40) << temp << A[i].gim.met << " " << A[i].gim.men << " " << A[i].gim.dien << " "
           <<  A[i].mir.met << " " << A[i].mir.men << " " << A[i].mir.dien << endl;
}
Last edited on
But that just prints space before words.
Then perhaps you need one of the other IO formatting flags? http://www.cplusplus.com/reference/ios/ios_base/fmtflags/

Look at the left and right adjustment flags. You will probably need to use one before you print the variable and the other after you print the variable.

Topic archived. No new replies allowed.