C++ programming please < Grocery List > Create a program that makes a grocery li
ID: 3807146 • Letter: C
Question
C++ programming please
< Grocery List >
Create a program that makes a grocery list.
You will need an array of strings.
You don’t 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?
//problem 1
#include
#include
#include
//define MAX items in list
#define MAX 1000
using namespace std;
//function to print the item list
void print(string s[], int n);
//declare function read text file
int read_grocery_text(string s[], int &n);
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
#include
#include
//define MAX items in list
#define MAX 1000
using namespace std;
//function to print the item list
void print(string s[], int n);
//declare function read text file
int read_grocery_text(string s[], int &n);
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;
}
}
-----------------------------------------------------------------------------
//problem 3
#include
#include
#include
//define MAX items in list
#define MAX 1000
using namespace std;
//function to print the item list
void print(string s[], int n);
//declare function read text file
int read_grocery_text(string s[], int &n);
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;
}
Explanation / Answer
Problem 1 & 2 seems fine
Problem 3 is missing the following:
1. Displaying the existing data in the file
2. Adding new items to the file at the end.
So for that the following are the modifications:
1. After int ret = read_grocery_text(items, n); following line can be added:
print(items, n);
2. After print(items, n + more_items); following can be added
ofstream out;
//open Grocery file for writing
out.open("Grocery.txt", std::ios_base::app);
//check if file is open
if (!out)
{
cout << "file can't be open for writing" << endl;
}
//write items to file
for (int i = n; i < n + more_items; i++)
{
out << items[i] << endl;
}
//out << EOF;
out.close();
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.