printing strings from a variable

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
54
55
56
57
58
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
 struct Database
    {
           string phone;
           string name;
           string address;
           string Email;
    };
   
    int main()
{
    char ch;
    ofstream Dbase;
    Dbase.open("Dbase.txt");
    
    
    
    Database PI;
   do{
    cout << "\n Please enter a name: ";
    getline(cin, PI.name);
    Dbase << PI.name;
    cout << " \n Please enter a phone number: ";
    getline(cin, PI.phone);
    Dbase <<"\n"<< PI.phone;
    cout << " \n Please enter an address: ";  
    getline(cin, PI.address);
    Dbase <<"\n"<< PI.address;
    cout << " \n Please enter an Email: ";
    getline(cin, PI.Email);
    Dbase <<"\n"<< PI.Email<<endl;
    Dbase.close();
    cout <<"would you like to review the information you just entered: y/n\n";
    cin >> ch;
    if(ch=='y'|ch=='Y')
    {
      goto Overhere;
    }
    cout <<"\nwould you like to create another entry: y/n\n";
    cin>>ch;
     }while(ch=='y' || ch=='Y');
     do{
     Overhere:
     ifstream infile;
     infile.open("Dbase.txt");
     infile >> PI.name>> PI.address >> PI.Email >> PI.phone;
     cout << PI.name <<"\n"<< PI.address <<"\n"<< PI.Email <<"\n"<< PI.phone;
     cout << "\n\nShould the program exit? y/n\n";
     cin >> ch;
    } while(ch=='n'|ch=='N');
    
    return 0;
    
}
 


As far as i can see I have 3 flaws i am asking for help with.

1) when the info is being read from files, and i try printing the strings stored in the variables i only get the first word from each string.

2) at the end of the first do while loop if the user decides to create another entry the program prints the line please enter a name, then immediately after in a new line with no space, the line please enter a phone number.

3) Finally every time a new entry is made it overwrites the previous one.

I would also like to know how to search for something in the file and all info relating to it, view the entire file, and delete the file.

Thanks for all the help in advance.

P.s. I'm using Dev C++ and running windows 7 ultimate, 32 bit.
Last edited on
1) operator>> will read until first whitespace character: one word in your case.
2) operator>> leaves whitespaces after last read entry including linefeed. getline() takes all simbols until it encounters linefeed, which happens immideatly in your case. Use cin.ignore() (http://cplusplus.com/reference/istream/istream/ignore/) to get around that.
MiiNiiPaa

1) I get that cout won't work so what shoud i use.

2) i don't follow.

P.s. the link is broken.
Last edited on
2) cin >> x;
you enter: "abc\n" (\n is an newline character, get in input stream after you press enter)
x now is: "abc"
input buffer: "\n" cin >> y;
operator>> skips whitespaces, input buffer ends and you are prompted to write another line: "cde \n"
y now is: "cde"
input buffer: " \n" getline(cin, z);
getline takes all symbols to firs encountered newline character, which gets discarded:
z now is " "
input buffer is empty.
fixed link:
http://cplusplus.com/reference/istream/istream/ignore/

1)You are usign getline in first part of the program exactly beacause of this.
thanks for taking the time to answer my questions miinipaa
1) It looks like you edited your original to use getline instead of cin >>. That should fix the problem of stopping at the first space.

2) The link is is okay if you exclude the trailing ")".
http://cplusplus.com/reference/istream/istream/ignore/

Line 40: It's a really bad practice to use goto to jump out of one loop into another loop. Use booleans instead.

Line 17: You don't check that your open of the file succeeded.
Line 35: You close your output file (Dbase) inside your loop, but don't reeopen it. Writes to the file will fail if you try to add another entry.

Informative thank you i will read up on booleans.

1)how would i check if the file opening succeed.
2)so i should close open the dbase, at the start of the loop. Thanks
1)
1
2
3
4
    if (! Dbase)
    {  cout << "Open of output file failed" << endl;
        exit(1);
    }


2) Open the output file at the beginning of the program before the loop as you're currently doing. Close the output file at the end of the program after the second loop (at line 54).


Thanks AbstractionAnon!
Topic archived. No new replies allowed.