Struct Problem CODE! :)
Nov 24, 2013 at 8:47am UTC
I am creating a struct program where one will input name, contact, item's details and etc, but the code has a problem and the problem is when inputting customer info 1 works well then the customer info 2 dont work then the output of customer info 1 wont show up properly but the cust info shows up properly. Help, please guide me! Thanks.
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
// Struct Prac_1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
struct Customer{
struct {
char firstName[20];
char lastName[20];
}name;
char contactNo[20];
struct {
struct {
int code;
char name[20];
double price;
}item;
struct {
int day;
int month;
int year;
}date;
int quantity;
}order;
};
void newline();
int main()
{
Customer yourCust[2];
cout<<"Enter 2 customers." ;
for (int i=0; i<2; i++)
{
cout<<"\n\nCUSTOMER INFORMATION " <<(i+1)<<endl;
cout<<"\nFirst Name: " ;
cin.getline(yourCust[i].name.firstName, 19);
cout<<"\nLast Name: " ;
cin.getline(yourCust[i].name.lastName, 19);
cout<<"\nContact No: " ;
cin.getline(yourCust[i].contactNo, 19);
cout<<"\n\nOrder Date." ;
cout<<"\nDay: " ;
cin>>yourCust[i].order.date.day;
cout<<"\nMonth: " ;
cin>>yourCust[i].order.date.month;
cout<<"\nYear: " ;
cin>>yourCust[i].order.date.year;
for (int i=0;i<2;i++)
{
cout<<"Enter 2 items." ;
cout<<"\n" <<i+1<<" ITEM" ;
cout<<"\nID: " ;
cin>>yourCust[i].order.item.code;
newline();
cout<<"\nName: " ;
cin.getline(yourCust[i].order.item.name, 19);
cout<<"\nPrice: " ;
cin>>yourCust[i].order.item.price;
newline();
cout<<"\nQuantity: " ;
cin>>yourCust[i].order.quantity;
}
}
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout<<"\nSUMMARY REPORT\n" ;
cout<<setw(5) <<"#" ;
cout<<setw(20) <<"Customer Name" ;
cout<<setw(12) <<"Order Date" ;
cout<<setw(12) <<"Items" ;
cout<<setw(14) <<"Price" ;
cout<<setw(17) <<"Quantity" ;
for (int i=0; i<2; i++)
{
cout<<endl;
cout<<setw(5) << i+1
<<setw(12) <<yourCust[i].name.lastName<<", " <<yourCust[i].name.firstName
<<setw(4) <<yourCust[i].order.date.month<<"/" <<yourCust[i].order.date.day<<"/" <<yourCust[i].order.date.year;
cout<<"\n" ;
}
for (int j=0; j<2; j++)
{
cout<<setw(19)<<"\t\t\t"
<<setw(9) <<yourCust[j].order.item.name
<<setw(14) <<yourCust[j].order.item.price
<<setw(17) <<yourCust[j].order.quantity;
}
system("pause>0" );
return 0;
}
void newline(){
char s;
do {
cin.get(s);
}while (s!='\n' );
}
Nov 24, 2013 at 10:09am UTC
You have two connected problems. First - you expect each customer struct to hold information about two items, but there is only one variable of type item in the order. You need to change your order substruct:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
struct {
struct {
int code;
char name[20];
double price;
int quantity; // <== this belongs here
} item[2]; // make it array of two,
//so 2 items can be purchased in one order
struct {
int day;
int month;
int year;
} date;
} order;
The other problem is that you have a nested for loop, but both levels use the same counter i which is updated in two different places.
1 2 3 4 5 6 7 8
for (int j=0; j<2; j++) // this is the inner loop - changed counter to j
{
cout<<"Enter 2 items." ;
cout<<"\n" <<j+1<<" ITEM" ;
cout<<"\nID: " ;
cin>>yourCust[i].order.item[j].code; // here access both items in the 'order'
// use i and j where they belong
}
Then of course you have to update the way you print your report to also use nested loop
Last edited on Nov 24, 2013 at 10:19am UTC
Nov 25, 2013 at 3:41am UTC
@jockX so my code now looks like this, but it still won't work. it still won't let me input the second customer's first name and the items inputted in the first customer won't show its output.
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
struct Customer{
struct {
char firstName[20];
char lastName[20];
}name;
char contactNo[20];
struct {
struct {
int code;
char name[20];
double price;
int quantity;
}item[2];
struct {
int day;
int month;
int year;
}date;
}order;
};
void newline();
int main()
{
Customer yourCust[2];
cout<<"Enter 2 customers." ;
for (int i=0; i<2; i++)
{
cout<<"\n\nCUSTOMER INFORMATION " <<(i+1)<<endl;
cout<<"\nFirst Name: " ;
cin.getline(yourCust[i].name.firstName, 19);
cout<<"\nLast Name: " ;
cin.getline(yourCust[i].name.lastName, 19);
cout<<"\nContact No: " ;
cin.getline(yourCust[i].contactNo, 19);
cout<<"\n\nOrder Date." ;
cout<<"\nDay: " ;
cin>>yourCust[i].order.date.day;
cout<<"\nMonth: " ;
cin>>yourCust[i].order.date.month;
cout<<"\nYear: " ;
cin>>yourCust[i].order.date.year;
for (int j=0;j<2;j++)
{
cout<<"Enter 2 items." ;
cout<<"\n" <<j+1<<" ITEM" ;
cout<<"\nID: " ;
cin>>yourCust[i].order.item[j].code;
newline();
cout<<"\nName: " ;
cin.getline(yourCust[i].order.item[j].name, 19);
cout<<"\nPrice: " ;
cin>>yourCust[i].order.item[j].price;
newline();
cout<<"\nQuantity: " ;
cin>>yourCust[i].order.item[j].quantity;
}
system ("cls" );
}
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout<<"\nSUMMARY REPORT\n" ;
cout<<setw(5) <<"#" ;
cout<<setw(20) <<"Customer Name" ;
cout<<setw(12) <<"Order Date" ;
cout<<setw(12) <<"Items" ;
cout<<setw(14) <<"Price" ;
cout<<setw(17) <<"Quantity" ;
for (int i=0; i<2; i++)
{
cout<<endl;
cout<<setw(5) << i+1
<<setw(12) <<yourCust[i].name.lastName<<", " <<yourCust[i].name.firstName
<<setw(4) <<yourCust[i].order.date.month<<"/" <<yourCust[i].order.date.day<<"/" <<yourCust[i].order.date.year;
cout<<"\n" ;
}
for (int j=0; j<2; j++)
{
cout<<setw(19)<<"\t\t\t"
<<setw(9) <<yourCust[j].order.item[j].name
<<setw(14) <<yourCust[j].order.item[j].price
<<setw(17) <<yourCust[j].order.item[j].quantity;
}
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
system("pause>0" );
return 0;
}
void newline(){
char s;
do {
cin.get(s);
}while (s!='\n' );
}
Nov 25, 2013 at 8:51am UTC
Try adding cin.ignore() after normal cin>> instructions (not the cin.getline()). Otherwise the newline at the end of the input (when you press enter to submit the input) gets saved for the next cin..
Nov 25, 2013 at 12:56pm UTC
@JockX now, the second name let's me input and is showing properly. But, the items of the first customer is not showing up. what do i do now? Thanks!
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
struct Customer{
struct {
char firstName[20];
char lastName[20];
}name[2];
char contactNo[20];
struct {
struct {
int code;
char name[20];
double price;
int quantity;
}item[2];
struct {
int day;
int month;
int year;
}date;
}order;
};
void newline();
int main()
{
Customer yourCust[2];
cout<<"Enter 2 customers." ;
for (int i=0; i<2; i++)
{
system ("cls" );
cout<<"\n\nCUSTOMER INFORMATION " <<(i+1)<<endl;
cout<<"\nFirst Name: " ;
cin.getline(yourCust[i].name[i].firstName, 19);
cout<<"\nLast Name: " ;
cin.getline(yourCust[i].name[i].lastName, 19);
cout<<"\nContact No: " ;
cin.getline(yourCust[i].contactNo, 19);
cout<<"\n\nOrder Date." ;
cout<<"\nDay: " ;
cin>>yourCust[i].order.date.day;
cin.ignore();
cout<<"\nMonth: " ;
cin>>yourCust[i].order.date.month;
cin.ignore();
cout<<"\nYear: " ;
cin>>yourCust[i].order.date.year;
cin.ignore();
for (int j=0;j<2;j++)
{
cout<<"Enter 2 items." ;
cout<<"\n" <<j+1<<" ITEM" ;
cout<<"\nID: " ;
cin>>yourCust[i].order.item[j].code;
cin.ignore();
newline();
cout<<"\nName: " ;
cin.getline(yourCust[i].order.item[j].name, 19);
cout<<"\nPrice: " ;
cin>>yourCust[i].order.item[j].price;
cin.ignore();
newline();
cout<<"\nQuantity: " ;
cin>>yourCust[i].order.item[j].quantity;
cin.ignore();
}
system ("cls" );
}
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout<<"\nSUMMARY REPORT\n" ;
cout<<setw(5) <<"#" ;
cout<<setw(20) <<"Customer Name" ;
cout<<setw(12) <<"Order Date" ;
cout<<setw(12) <<"Items" ;
cout<<setw(14) <<"Price" ;
cout<<setw(17) <<"Quantity" ;
for (int i=0; i<2; i++)
{
cout<<endl;
cout<<setw(5) << i+1
<<setw(12) <<yourCust[i].name[i].lastName<<", " <<yourCust[i].name[i].firstName
<<setw(4) <<yourCust[i].order.date.month<<"/" <<yourCust[i].order.date.day<<"/" <<yourCust[i].order.date.year;
cout<<"\n" ;
}
for (int j=0; j<2; j++)
{
cout<<setw(19)<<"\t\t\t"
<<setw(9) <<yourCust[j].order.item[j].name
<<setw(14) <<yourCust[j].order.item[j].price
<<setw(16) <<yourCust[j].order.item[j].quantity;
}
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
system("pause>0" );
return 0;
}
void newline(){
char s;
do {
cin.get(s);
}while (s!='\n' );
}
Nov 25, 2013 at 9:06pm UTC
What is the purpose of this?
1 2 3 4 5 6
void newline(){
char s;
do {
cin.get(s);
}while (s!='\n' );
}
Topic archived. No new replies allowed.