Mar 23, 2014 at 12:36am UTC
I wrote this program to help me create a list of medical resources and their attributes, a task I have been performing repeatedly lately. I'm still fairly new to C++, so I thought to create a structure "Resource", and then an array of those structures "city[300]". My problem is that the input doesn't seem to be happening: the program runs, but when it prints to screen/writes to the file at the end, all the shows is:
Resource Type:
Name:
Address:
Phone:
Website:
for every resource that was input. All the fields are blank. I'm not sure what I'm doing wrong, please 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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct Resource
{
string typeID;
string name;
string address;
string phone;
string website;
string description;
};
int main()
{
Resource city[300];
bool next = true ;
int i = 0;
while (next)
{
cout << "\nType ID: " ;
getline(cin,city[i].typeID) ;
cout << "Name: " ;
getline(cin,city[i].name) ;
cout << "Address: " ;
getline(cin,city[i].address) ;
cout << "Phone: " ;
getline(cin,city[i].phone) ;
cout << "Website: " ;
getline(cin,city[i].website) ;
cout << "Description: " ;
getline(cin,city[i].description) ;
i++;
cout << "\nContinue? :" ;
cin >> next;
cout << endl << endl;
}
ofstream myfile("sanmateo.txt" );
for (int j=0;j<i;j++)
{
cout << "\nResource Type: " << city[i].typeID;
cout << "\nName: " << city[i].name;
cout << "\nAdress: " << city[i].address;
cout << "\nPhone: " << city[i].phone;
cout << "\nWebsite: " << city[i].website;
cout << "\n" << city[i].description;
cout << "\n\n" ;
myfile << "\nResource Type: " << city[i].typeID;
myfile << "\nName: " << city[i].name;
myfile << "\nAdress: " << city[i].address;
myfile << "\nPhone: " << city[i].phone;
myfile << "\nWebsite: " << city[i].website;
myfile << "\n" << city[i].description;
myfile << "\n\n" ;
}
myfile.close();
return 0;
}
Last edited on Mar 23, 2014 at 12:45am UTC
Mar 23, 2014 at 3:09am UTC
add
cin.ignore()
after
cin >> next
http://www.cplusplus.com/forum/beginner/76273/#msg409534
and a minor suggestion :
a safer version of the while loop :
1 2 3 4 5 6 7 8 9
while ( next )
{
// ... codes
if ( ++i == 300 ) break ;
// ...
}
just in case the user wants to enter more than 300 entries
Last edited on Mar 23, 2014 at 3:15am UTC
Mar 23, 2014 at 4:49am UTC
oh yes, why didn't i think of that ://
Mar 23, 2014 at 4:50am UTC
Alternately, use
std::vector or
std::deque . This allows for the size to keep growing indefinitely, and prevents wasted memory for small numbers:
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
#include <vector>
// ...
int main() {
std::vector<Resource> city;
bool next = true ;
while (next) {
Resource temp;
std::cout << "\nType ID: " ;
std::getline(std::cin, temp.typeID);
std::cout << "Name: " ;
std::getline(std::cin, temp.name);
// etc...
city.push_back(temp);
std::cout << "Continue? " ;
std::cin >> next;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n' );
std::cout << "\n\n" ;
}
std::ofstream myfile("..." );
for (Resource res : city) {
std::cout << "\nResource Type: " << res.typeID;
std::cout << "\nName: " << res.name;
// etc...
}
return 0;
}
Note that you will need a C++11 compliant compiler to compile this (all modern compilers are), if you don't have one change the last for loop to:
for (std::vector<Resource>::const_iterator it = city.cbegin(); it != city.cend(); ++it)
And change all the
. 's to
-> (e.g.
std::cout << "\nResource Type: " << it->typeID;
)
Last edited on Mar 23, 2014 at 4:51am UTC