nooby mistake hopefully

i have two problems atm.

1) the first one is for some reason it skips the first question "clients name"
2) the secord one is i cant get it to save the questions to the array,( its only does numbers but not chars )

please compile and say where ive gone wrong :)

heres the code

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
 // basic file operations
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <windows.h>
#include <sstream>
#include <string>
using namespace std;
int menu[4] ;
int client[6];
int item[5];
 string filename; 
 
 void delay()
 {
 int time =1;
 
 for(time != 500; time++;){
          system("cls");
         
          break;
          
          }
          
}

void menu1()
{
     cout << "What would you like to do? \n \n";
     cout << "   [1] start a new file. \n";
     cout << "   [2] Load a file. \n";
     cout << "   [3] Edit a file. \n";
     cout << "   [4] Edit the header file. \n\n";
     cout << "---PLease enter a number (1-4)--- \n";
    
     
     cin >> menu[0];
      
      delay();
     
  
     }
 
 void menu1op1()
 {   
      string mystr;
      
         ofstream myfile;
    cout << "What would you like to call this file:  " ;
    cin >> filename;  
  myfile.open((std::string(filename) + ".txt").c_str());
 



delay();
cout << "Client name: "; // SKIPS THIS ONE
 
 getline (cin, mystr);
    stringstream(mystr) >> client[0];
   
   delay();
    
 
 cout << "Contact phonenumber: ";
 
 getline (cin, mystr);
    stringstream(mystr) >> client[1];
   delay();
    
 cout << "Address line one: ";
 
 getline (cin, mystr);
    stringstream(mystr) >> client[2];
  
   delay();
 
 
 cout << "Address line two: ";
 
 getline (cin, mystr);
    stringstream(mystr) >> client[3];
    
    delay();
 
 
 
 cout << "Address line three: ";
 
 getline (cin, mystr);
    stringstream(mystr) >> client[4];
    
 delay();

 
 cout << "Postcode: ";
 

 getline (cin, mystr);
    stringstream(mystr) >> client[5];
    
    delay();

 
 cout << "Is this correct? \n";
 cout << client[0] << "\n";
  cout << client[1] << "\n";
   cout << client[2] << "\n";
    cout << client[3] << "\n";
     cout << client[4] << "\n";
      cout << client[5] << "\n";
      
      
 
 myfile << client[0] << "\n";
  myfile << client[1] << "\n";
   myfile << client[2] << "\n";
    myfile << client[3] << "\n";
     myfile << client[4] << "\n";
      myfile << client[5] << "\n";    // DOESNT SAVE TO THE ARRAYS AS IT SHOULD.
 
  myfile.close();
     
}

int main () {
   
   menu1();
   
   switch(menu[0]){
                  case 1:
                       menu1op1();
                       break;
                       }
                       system("pause");
  return 0;
}
 
Last edited on
Use getline() not cin >> x. Or you have to flush the buffer of newline characters :) This should solve your problem.
Also, client is an int. So it can only hold a number.
thanks zaita but i have already used getline() i think etc that one at the top which i have just changed...

anyway i got the same errors and what can i used instead of a int?
Mixing getline and cin >> will cause issues if you don't use cin.ignore().

You have to think about what you're saving. You're not saving ints (numbers), so why is clients an int? What should clients be?

oh okay thanks

words and numbers?
I am a student at UAT online, hopefully I can help.

at line 52 add

cin.ignore(1) ;

this will flush the gratuitous enter from the buffer and allow you to see the next line rather than cycling through to the client number.

thanks that sloved problem one :)

now what data type can i used instead of int?
string
how?
string client[6]; instead of int client[6];
is their another way when using strings's for getline? or cin?
okay i put this together to simple it down. atm it only can save one word to a array... how can i save a sentence.
also i got to be able t know how to load it back in, a way that doesnt depend on how many words someoen enters.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>
using namespace std;

int main ()
{
    string test[5];
cout << "enter \n";
cin >> test[5];
cout << test[];
system("pause");
  return 0;
}
system("pause") is non-standard and should never be used.
okay i will take it out but stll anoyone know how to answer the questions xD
1) Why is menu an array?
2) item appears unused.

You clearly need to do some reading on C++ arrays. You example above is likely to cause a crash. Before you tell me you've already read it, read it again and you'll notice while your array example above is wrong.

http://www.cplusplus.com/doc/tutorial/arrays/
because i didnt get around to making more use off it
Topic archived. No new replies allowed.