Two dimensional string array display problem

I want a string to be displayed from a two dimensional array. But the output shows only the first part i.e before the space present in the string.

i/p : Break the the egg.
o/p : Break

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
  #include<iostream.h>
#include<conio.h>
#include<process.h>

const int size=20;

void main()
{
 clrscr();
 char rname[20];
 char ingredients[size][50];
 char unit[size][5];
 int quantity[size];
 char instructions[size][50];
 int qcount,icount;
 int n;

 cout<<"\nEnter Recipe name :";
 cin>>rname;
 cout<<"\nEnter No.of.Ingredients :";
 cin>>n;
 qcount=n;
 while(n>0)
 {
	cout<<"\nEnter quantity unit and ingredients ";
	cin>>quantity[n]>>unit[n]>>ingredients[n];
	--n;
 }
 cout<<"\nEnter No. Of Instructions :";
 cin>>n;
 icount=n;
 cout<<"\nEnter Instructions below :\n";
 while(n>0)
 {
  cin>>instructions[n];
  --n;
 }
 cout<<"\nRecipe Details :\n";
 cout<<rname<<endl;
 for(int i=qcount;i>0;i--)
 {
  cout<<quantity[i]<<" "<<unit[i]<<" "<<ingredients[i]<<endl;
 }
 for(i=icount;i>0;i--)
 {
	 cout<<instructions[i]<<endl; //i can get full string displayed here.
 }

 getch();
}
But the output shows only the first part i.e before the space present in the string

That sounds like the symptoms of an input problem. To read a string containing spaces, you need getline().

http://www.cplusplus.com/reference/istream/istream/getline/

Question: Are you using Turbo C++ ?

If so, then getline might not exist.
Last edited on
Line 1: Correct header is <iostream>

Line 7: main must return type int.

Line 35: cin only reads until the first whitespace. It leaves anything after the first whitespace in the input buffer.
http://www.cplusplus.com/doc/tutorial/basic_io/
Note: This also applies at line 26 if ingredients can have embedded whitespace.

I strongly recommend that you use std::string instead of C character arrays.
std::getline() will read up to the end of line (by default) allowing the user to enter whitespace.

You don't check that the value the user enters for n is greater than size. This could cause out of bounds references and undefined behavior. I also strongly recommend the use of std::vector. Using std::vector will remove the maximum limitation on the number of ingredients and instructions,

Get rid of Turbo C and get a decent standards compliant compiler.

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
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{   string          rname;
    vector<string>  ingredients;
    vector<string>  units;
    vector<int>     quantity; 
    vector<string>  instructions;
    int     qcount,icount;
    int     qty;
    string  unit;
    string  ingred;
    string  instruct;
    
    cout << "\nEnter Recipe name :";
    cin >> rname;
    cout << "\nEnter No.of.Ingredients :";
    cin >> qcount;
    for (int i=0; i<qcount; i++)
    {   cout<<"\nEnter quantity unit and ingredients ";
	    cin>>qty>>unit>>ingred;
	    quantity.push_back(qty);
	    units.push_back (unit);
	    ingredients.push_back (ingred);
	}
    
    cout<<"\nEnter No. Of Instructions :";
    cin>>icount; 
    for (int i=0; i<icount; i++)
    {   cout<<"\nEnter Instructions below :\n";
        getline (cin, instruct); 
        instructions.push_back (instruct); 
    }
 
    cout<<"\nRecipe Details :\n";
    cout<<rname<<endl;
    for(int i=0; i<qcount; i++) 
    {   cout<<quantity[i]<<" "<<units[i]<<" "<<ingredients[i]<<endl;
    }
    for(int i=0; i<icount; i++) 
    {   cout<<instructions[i]<<endl; 
    }
    return 0;
 }
Last edited on
I cant input values for instructions... it just gets the input for no. of instructions and displays the details right away..

After cin>>icount; (line 31) there will be a trailing newline '\n' left in the input buffer. That causes the next getline to read an empty string.

You can remove it with cin.ignore();
or cin.ignore(1000, '\n';);.

The second version effectively discards everything until a newline is found (up to a limit of 1000 characters in this case, the exact number usually doesn't matter).
Topic archived. No new replies allowed.