Hi everyone, I am learning C++ and I must write a program that prints the contents of a struct called Article. So far so good. Any article has the following characteristics:
-article number
-quantity
-description (20 characters)
The test program must create an article of which the contents are assigned at initialization level.
Printing the article is done with a print() function. This function gets the address of the structure as a parameter.
//article_printing.cpp
//the program creates a struct "article" that contains the article number, quantity and description
//then it is printed using function print()
// Preprocessor for include files
#include <iostream> // C++ style I/O using operator
usingnamespace std; // The C++ logical collection of functions
#define MAXLINE 20
struct article{
int article_number;
int quantity;
char description[MAXLINE+1];
};
void print(article toprint){
cout<<endl<<"The article has the following characteristics: "<<endl;
cout<<"Number: "<<toprint.article_number<<endl;
cout<<"Quantity: "<<toprint.quantity<<endl;
cout<<"Description: "<<toprint.description<<endl;
}
void main() {
//initialize variables
article an_article; //creates a struct called "an_article"
cout<<"Please enter the article number: "<<endl;
cin>>an_article.article_number;
cout<<"Please enter the quantity: "<<endl;
cin>>an_article.quantity;
cout<<"Please type the article description: "<<endl;
cin>>an_article.description; //problem!!!! it only prints the first word entered.
print(an_article); //calls the print function
}
Here are my questions:
FIRST: Am I creating the Article of which contents are assigned at initialization level?
SECOND: Why can't I set a description that has more than two words?
Thank you for your prompt reply iHutch105. I changed line 32 with your suggestion and it skips it all the way, so when printing, the description is printed empty.
If by initialization level means construction then to answer your first question no.
cin>>an_article.article_number; is an assignment. If this is for a class it may mean you are supposed to pass these into a constructor (this is just a side note).
int main() {
int ar, qu ;
string des;
cout<<"Please enter the article number: "<<endl;
cin>>ar;
cout<<"Please enter the quantity: "<<endl;
cin>>qu;
cout<<"Please type the article description: "<<endl;
cin.ignore();
getline(cin, des); //problem!!!! it only prints the first word entered.
constchar* p ;
p = des.c_str();
article an_article( ar , qu , p );//initialize variables
//creates a struct called "an_article"
print(an_article); //calls the print function
return 0;
}