Hi im currently doing some work with arrays and ive been working on this program.
The goal is a user can add up to three products. Each product will have a name, model number and price. Once all is entered a list will pop out showing all the products with their respective model number and price. my part1 of the code allows the user to do the inputs and part shows the output.

#include <iostream>
using namespace std;

char product[10][100];
char model[10][100];
char price[10][100];

int first();
int second();

int main()
{
first();    
second();
return 0;
}


int first()
{
for (int i = 0; i < 3; i++)
{
cout << "Please enter up to ten products: ";
cin >> product[i];
cout << "Please enter up to ten model numbers: ";
cin >> model[i];
cout << "Please enter the prices: ";
cin >> price[i];
}

}
  
int second()
{
 cout<<"\n";
 cout<<product[0]<<" "<<model[0]<<" "<<price[0]<<" "<<endl;
 cout<<product[1]<<" "<<model[1]<<" "<<price[1]<<" "<<endl;
 cout<<product[2]<<" "<<model[2]<<" "<<price[2]<<" "<<endl;
 system("pause");
return 0;
system("exit");
}

This is all working fantastic however, what if a user wanted to end the loop early?
I want a way so that when prompted to enter the product name, the user can type "quit" and the loop will break.

Example. a User will enter one product with its model number and price but when asked again another product, he will type "quit". The loop will end and the information about his one and only product will be displayed.

ive tried using if statements and while loops but i have no idea. any help is appreciated. if you wish to see these failed codes i can put them up also.

any help is much appreciated

Recommended Answers

All 3 Replies

this is an updated one.

here its in its final stages but the if command inside will not work. i think its the last step needed. any help is greatly appreciated.

#include <iostream>
#include <string>
using namespace std;

string product[10];
string model[10];
string price[10];
string red; 


int second();

int main()
{

for (int i = 0; i < 3; i++)
{
cout << "Please enter up to ten products: ";
cin >> red;
product[i]=red;

if(red=='quit')
break;

cout << "Please enter up to ten model numbers: ";
cin >> model[i];
cout << "Please enter the prices: ";
cin >> price[i];


}

}


   
      
second();
return 0;
}

int second()
{
 cout<<"\n";
 cout<<product[0]<<" "<<model[0]<<" "<<price[0]<<" "<<endl;
 cout<<product[1]<<" "<<model[1]<<" "<<price[1]<<" "<<endl;
 cout<<product[2]<<" "<<model[2]<<" "<<price[2]<<" "<<endl;
 system("pause");
return 0;
system("exit");
}

Compare with "quit" instead of 'quit'.
g++ gave this output:

quit.c:22:11: error: multi-character character constant
quit.c: In function ‘int main()’:
quit.c:22:11: error: no match for ‘operator==’ in ‘red == 1903520116’


Click here for full review.

Pieter

this is an updated one.

here its in its final stages but the if command inside will not work. i think its the last step needed. any help is greatly appreciated.

#include <iostream>
#include <string>
using namespace std;

string product[10];
string model[10];
string price[10];
string red; 


int second();

int main()
{

for (int i = 0; i < 3; i++)
{
cout << "Please enter up to ten products: ";
cin >> red;
product[i]=red;

if(red=='quit')
break;

cout << "Please enter up to ten model numbers: ";
cin >> model[i];
cout << "Please enter the prices: ";
cin >> price[i];


}

}


   
      
second();
return 0;
}

int second()
{
 cout<<"\n";
 cout<<product[0]<<" "<<model[0]<<" "<<price[0]<<" "<<endl;
 cout<<product[1]<<" "<<model[1]<<" "<<price[1]<<" "<<endl;
 cout<<product[2]<<" "<<model[2]<<" "<<price[2]<<" "<<endl;
 system("pause");
return 0;
system("exit");
}

that worked out really well!

with that my code is perfect and works just the way i wanted. thank you very much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.