Question: C++ programming please < Grocery List > Create a program that makes a grocery list. You will need an array of strings. You dont know
C++ programming please < Grocery List > Create a program that makes a grocery list. You will need an array of strings. You dont know how many items will be on your list.
1) Simple problem: User tells you how many items, then tells you the items. You display the items for the user.
2) More complex problem: User tells you how many items, then tells you the items. You display the items for the user. At the end of the program, write the grocery list to a text file called grocery.txt.
3) Even more complex problem: If grocery.txt exists, read it into an array and display to the user. User then tells you how many more items, and then tells you the items. You display all the items for the user. At the end of the program, write the grocery list to a text file called grocery.txt. Start with the simple program, move toward the complex one. ------------------------------------------------------------------------------------------
This is my code,but professor said "missing part 3" and I got gradeB. could you change my code?
//1st problem
#include
int main() { string items[MAX]; int n,more_items = 0;
cout << "Enter number of items: "; cin >> n; cin.ignore(); //ask the user about each item name for (int i = 0; i < n; i++) { cout << "item" << i + 1 << " :"; getline(cin, items[i]); } print(items, n); }
//function definitin for displaying item list void print(string s[],int n) { cout << " Display items: " << endl; for (int i = 0; i < n; i++) { cout << i+1<<". " << s[i] << endl; } }
//problem2
#include
int main() { string items[MAX]; int n,more_items = 0;
cout << "Enter number of items: "; cin >> n; cin.ignore(); //ask the user about each item name for (int i = 0; i < n; i++) { cout << "item" << i + 1 << " :"; getline(cin, items[i]); } print(items, n); ofstream out; //open Grocery file for writing out.open("Grocery.txt"); //check if file is open if (!out) { cout << "file can't be open for writing" << endl; } //write items to file for (int i = 0; i < n; i++) { out << items[i] << endl; } //out << EOF; out.close(); }
//function definitin for displaying item list void print(string s[],int n) { cout << " Display items: " << endl; for (int i = 0; i < n; i++) { cout << i+1<<". " << s[i] << endl; }
}
//proble3
#include
int main() { string items[MAX]; int n,more_items = 0;
//to read from file and store in array use below code int ret = read_grocery_text(items, n); cout << "Enter number of more items: "; cin >> more_items; //ask the user about each item name cin.ignore(); for (int i = n; i < n + more_items; i++) { cout << "item" << i + 1 << " :"; getline(cin, items[i]); } print(items, n + more_items);
}
//function definitin for displaying item list void print(string s[],int n) { cout << " Display items: " << endl; for (int i = 0; i < n; i++) { cout << i+1<<". " << s[i] << endl; } }
int read_grocery_text(string s[], int &n) { ifstream in; //declare a variable to hold number of items int count = 0; //open input file grocery.txt in.open("Grocery.txt"); //check if file can be open if (!in) { cout << "file can't be open for reading" << endl; return -1; } while (!in.eof()) { getline(in, s[count]);
if (in.eof()) break; ++count; } n = count; return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
