string concatenation coding problem.

In this program, i am trying concatenate different inputs that have been defined by the user. The problem comes in when i am trying to make a phrase such as "This is a test" where the output will only be "Thisisatest". I am not too sure how to go about including white spaces, i have tried to use noskipws and strcat with no success. Thanks in advance.



void printArray (char arg [], int i)
{
int j = 0;

while (j < i)
{
cout << arg [j];

j = j + 1;
}

cout << endl;
}

void ptrPrintArray ( char arg [], int j)
{
int i = 0;
char * ptr;
ptr = arg;

while (i < j)
{
cout << *ptr;
ptr ++;

i = i + 1;
}

}


int main ()
{
char chararray [80];
char inputchar;
int i = 0;

cout << "Input char: ";
cin >> inputchar;

while (inputchar != '$')
{
chararray [i] = inputchar;
i = i + 1;
cin >> inputchar;
}


printArray (chararray, i);
ptrPrintArray (chararray, i);

return 0;
}
I did not find any string concatenation in your code but can advice to use std::cin.get( inputchar )
In this case the white spaces are indeed accounted for, but a line is skipped after each entry in the output.

i.e "This is a test" becomes:

This
is
a
Test
Here is the solution.
1) You have to use string instead of char.
2) Then you have to use getline (cin, inputchar);
I have not changed the variable names, just changed the type.
I hope you will like it.
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
#include <iostream>
#include <string>
using namespace std;
void printArray (string arg [], int i)
{
	int j = 0;

	while (j < i)
	{
		cout << arg [j];

		j = j + 1;
	}

	cout << endl;
}

void ptrPrintArray ( string arg [], int j)
{
	int i = 0;
	string * ptr;
	ptr = arg;

	while (i < j)
	{
		cout << *ptr;
		ptr ++;

		i = i + 1;
	}

}


int main ()
{
	string chararray [80];
	string inputchar;
	int i = 0;

	cout << "Input char: ";
	getline(cin, inputchar);

	while (inputchar != "$")
	{
		chararray [i] = inputchar;
		i = i + 1;
		getline(cin, inputchar);
	}


	printArray (chararray, i);
	ptrPrintArray (chararray, i);
	return 0;
} 
ahhhh yes that would make sense, i originally started with strings but then changed to chars thinking it would consider white spaces as a character. Thanks a bunch guys =D
ahhhh yes that would make sense, i originally started with strings but then changed to chars thinking it would consider white spaces as a character. Thanks a bunch guys =D


What do you mean? Whitespaces are characters...


Something more, your concept about char, if you use cin >> with char, then i tell you that cin just takes the input before the first white space, if you give input as "this is" then it has just took "this" as input

while in case vlad told above cin.get takes enter as input too with given char.
Have fun!
Last edited on

What do you mean? Whitespaces are characters...

White spaces are character, but cin just takes input before first whitespace character.
Topic archived. No new replies allowed.