please use C programming Create a program that makes a groce

please use C++ programming

<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.

Simple problem: User tells you how many items, then tells you the items. You display the items for the user.

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.

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.

Solution

//1st problem

#include<iostream>
#include<fstream>
#include<string>
//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<iostream>
#include<fstream>
#include<string>
//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;
   }



}

//proble3

#include<iostream>
#include<fstream>
#include<string>
//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;
}

-------------------------------------------------------------------------

//output1

Enter number of items: 5
item1 :rice
item2 :coconut oil
item3 :jaggary
item4 :sugar
item5 :dal

Display items:
1. rice
2. coconut oil
3. jaggary
4. sugar
5. dal

//output2

Grocery.txt contains

rice
jaggary
coconut oil
ground nut
dal

//output3

Enter number of more items: 2
item6 :sugar
item7 :gram dal

Display items:
1. rice
2. jaggary
3. coconut oil
4. ground nut
5. dal
6. sugar
7. gram dal

please use C++ programming <Grocery List> Create a program that makes a grocery list. You will need an array of strings. You don’t know how many items wil
please use C++ programming <Grocery List> Create a program that makes a grocery list. You will need an array of strings. You don’t know how many items wil
please use C++ programming <Grocery List> Create a program that makes a grocery list. You will need an array of strings. You don’t know how many items wil
please use C++ programming <Grocery List> Create a program that makes a grocery list. You will need an array of strings. You don’t know how many items wil

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site