rewrite his function

Hello friends,

I am writing this program to get input in string and to output it as string but only the amount of string input provided not the MAXSIZE i provided. How can you re write it to do such a thing ? when I put the following code it actually print out all of MAXSIZE even the empty index once.
1
2
3
4
5
6
7
void displayNames(string a[], int& n)
{
	for(int x=0; x < MAXSIZE; x++)
	{
	cout << " The array enter is " << a[x] << endl;
	}	
}


Is this an alternative way that works ? thank you.
1
2
3
4
5
6
7
8
9
10
11

void displaynames (string arr[], int n) 
{
     for(int i=0; i < sizeof(arr);i++)
     {
         cout << arr[i] << endl;
     }

     cout<< i <<" names entered" <<endl;
}
how about this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;

int main()
{
	
	string Answer;
	int x = 0;
	
	cout << "say something. :";
	cin >> Answer;
	
	x = Answer.size();
	
	cout << "The Array enter is " << x << endl;
		
 	return 0;
}
Last edited on
That works thanks a lot Shifty .
this at least will give you the length of a word entered by the user, but it only works for a single word.
I already have a function to input the statement I just want to be able to display it though .
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
#include <iostream>
using namespace std;
const int MAXSIZE=20;
void getData(string a[], int n)
{
	n=0;
	do
	{
		cout<<"Enter a string, quit to terminate:";
		cin>>a[n];
	}while((a[n++]!="quit")&&(n<MAXSIZE));
	if(n<MAXSIZE)
		{--n;}
}
void displayNames(string a[], int& n)
{
	for(int x=0; x <= sizeof(n); x++)
	{
	cout << " The array enter is " << a[x] << endl;
	}	
}
char getMenuResponse()
{
	char response;
	cout << endl << "Make your selection" << endl
		 << "(A)dd Record, (D)isplay name, (F)ind name, (S)wap names, (R)emove name , (Q)uit" << endl
		 << "> ";
	cin >> response;
	cin.ignore(256, '\n');	
	return toupper(response);
}
int linSearch(int t, int a[], int n)
{
	bool found=false;
	for(int i=0; i<n; i++)
	{
		if(a[i]==t)
		{
			found=true;
			return i;
		}
	}
	if(!found)
		return -1;
}
void bSort(string a[], int n)
{
	for(int i=1; i<n; i++)
		for(int j=0; j<n-i; j++)
			if(a[j]>a[j+1])
				swap(a[j],a[j+1]);
}
int bSearch(string t, string a[], int n)
{
	bool found=false;
	int lo=0,
	 hi=n;
	int i;
	while(lo<=hi)
	{
		i=(lo+hi)/2;
		if(a[i]==t)
		{
			found=true;
			return i;
		}
		else if(t>a[i])
			lo=i+1;
		else
			hi=i-1;
	}
	if(!found)
		return -1;
}
int main()
{
	string arr[MAXSIZE];
	int n;
	string ch;
	bool run = true;
	do
	{
		  	switch (getMenuResponse()) 
	  	{
	  		case 'A': getData(arr,n); break;
	  		case 'F': bSearch(ch, arr, n); break;
	  		case 'D': displayNames(arr,n); break;
	  		case 'S': bSort(arr,n); break;
	  		case 'R': getData(arr,n); break;
	  		case 'Q': run = false; break;
	  		default : cout << "That is NOT a valid choice" << endl;
	  	}
	}while(run);
  	cout << endl << "Program Terminated" << endl;	

}
I don't think i fully understand what your looking for, but it sounds like the .size() is what you need.

http://www.cplusplus.com/reference/string/string/size/
Topic archived. No new replies allowed.