Problem reading out array

I am having difficulties reading out the array properly. I believe my function is reading and adding an 's' to every letter instead of at the very end. I have no idea how to go about correcting the problem. Any tips?

This is what I am getting when running the program
1
2
3
4
Enter word:
tom
╠╠╠
Press any key to continue . . .


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
  /* Write a function with two C++ strings as parameters. 
 void addS(const char orig[], char plural[]) 
The function will make a copy of orig to the C++ string plural 
with an ‘s’ at the end. A sample driver would be: 
 
char word[10]; 
addS(“school”, word); 
cout << word << endl; //will output schools
*/

/**
* @author Caleb Ozier
* @file stringCompare.cpp */

#include <iostream>
#include <iomanip>
using namespace std;

void addS(const char orig[], char plural[]);

int main()
{

  /*  char orig[10];
	char plural[10];
	
	cout << "Enter word: " << endl;
	cin >> orig;
	
	addS(orig, plural);
	cout  << plural << endl;
    */

    char x[10];
	char word[10];
	
	cout << "Enter word: " << endl;
	cin >> x;
	
	addS(x, word);
	cout << word << endl;

return 0;
} // end main driver

void addS(const char orig[], char plural[])
{

	int index = 0;
	while (orig[index] != NULL)
	{
		index++;	
	}

	for (int i = 0; i < index; i++)
	{
            
			plural[index] = orig[index+1] + 's';
	        plural[index] = '\0';
	   

	}

}
In this piece of code, you are accessing the same index over and over,then saying it is equal to "orig[index + 1] + 's' ". After such act, you set its value to null('\0'),quite redundant.

1
2
3
4
5
6
7
8
9
10
11
void addS(const char orig[], char plural[])
{
	for (int i = 0; i < index; i++)  
	{
            
			plural[index] = orig[index+1] + 's';
	        plural[index] = '\0';   

	}

}







This should fit your needs.

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
#include <iostream>



void adds(const char source[],char destination[])
{
    while(*source)
    {
        *destination++ = *source++;
    }
    *destination++ = 's';
    *destination = '\0';
}

int main()
{
    char source[10];
    char destination[10];

    std::cin >> source;

    adds(source,destination);

    std::cout << destination << std::endl;
    return 0;
}
Topic archived. No new replies allowed.