Feb 15, 2011 at 9:27pm UTC
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 Feb 15, 2011 at 9:42pm UTC
Feb 15, 2011 at 10:00pm UTC
Use getline()
not cin >> x
. Or you have to flush the buffer of newline characters :) This should solve your problem.
Feb 15, 2011 at 10:00pm UTC
Also, client is an int. So it can only hold a number.
Feb 15, 2011 at 10:56pm UTC
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?
Feb 15, 2011 at 11:10pm UTC
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?
Feb 15, 2011 at 11:26pm UTC
oh okay thanks
words and numbers?
Feb 15, 2011 at 11:32pm UTC
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.
Feb 16, 2011 at 12:27am UTC
thanks that sloved problem one :)
now what data type can i used instead of int?
Feb 16, 2011 at 10:52am UTC
string client[6];
instead of int client[6];
Feb 16, 2011 at 11:01am UTC
is their another way when using strings's for getline? or cin?
Feb 16, 2011 at 11:14am UTC
system("pause") is non-standard and should never be used.
Feb 16, 2011 at 11:25am UTC
okay i will take it out but stll anoyone know how to answer the questions xD
Feb 21, 2011 at 3:41pm UTC
because i didnt get around to making more use off it