Structures problem

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.

So far, this is what I've wrote:

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
//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 
using namespace 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?

what am I doing wrong?

thanks.
Last edited on
I'm not quite sure what you mean by your first question.

As for 2, this should probably work:

1
2
// cin>>an_article.description;
cin.getline(an_article.description, MAXLINE);
Last edited on
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.

Could anyone explain me why is this happening?

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

What do you mean it skips?
it is just as if no code was written and just prints the cout line but it does not ask the user to input any text.
That is strange, try cleaning the solution and rebuilding. It is working for me.
Hi
first change your struct, add a constructor for example;

1
2
3
4
5
6
7
8
9
10
11
struct article{

	article(int ar, int qu, const char* des){
		article_number = ar, quantity = qu ;
		strncpy(description, des, MAXLINE );
	}

	int article_number;
	int quantity;
	char description[ MAXLINE + 1 ];
};


than get the whole line by using getline, and call cin.ignore(); before you read

and change your print param to take a const reference like

void print(const article& toprint){//do somthing}

and finally your main should look like following

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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.
	const char* 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;
}


make sure you add

1
2
#include <string>
 using namespace std;


hope it helps
Topic archived. No new replies allowed.