Error. Plural program C++

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 5237 Views - Last Post: 31 March 2011 - 06:43 PM Rate Topic: -----

#1 starkhavenprincessrin   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 31-March 11

Error. Plural program C++

Posted 31 March 2011 - 05:16 PM

okay so I need to write a function with two C++ strings as parameters. void addS( const char orig[], char plural[]). Then the function has to make a copy of orig to the C++ sting plural with an 's' at the end.

Here's my code so far:

# include <iostream>
using namespace std;

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


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

void addS(const char orig[], char plural[])
{
	int index = 0;
	while (orig[index] != NULL)
	{
		index++;
		for ( int i = 0; i > index; i++)
		{
			plural[i] = orig[i] + 's';
		}
	}
}



When I run the program it gives me this:

Enter word:
school
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠school
Press any key to continue . . .

I'm not sure what I did wrong. I think it might have to do with coping orig and then adding the s.

Thank you.

Rin

Is This A Good Question/Topic? 0
  • +

Replies To: Error. Plural program C++

#2 JaKWaC   User is offline

  • D.I.C Head

Reputation: 76
  • View blog
  • Posts: 234
  • Joined: 15-November 10

Re: Error. Plural program C++

Posted 31 March 2011 - 05:29 PM

Firstly, char arrays are not c++ strings. So do you really need to use c++ strings?
Was This Post Helpful? 1
  • +
  • -

#3 starkhavenprincessrin   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 31-March 11

Re: Error. Plural program C++

Posted 31 March 2011 - 05:33 PM

View PostJaKWaC, on 31 March 2011 - 05:29 PM, said:

Firstly, char arrays are not c++ strings. So do you really need to use c++ strings?


This is how my instructor wants us to build the program.
I think it would be easier to just use strings but she wants us to use arrays.
Was This Post Helpful? 0
  • +
  • -

#4 JaKWaC   User is offline

  • D.I.C Head

Reputation: 76
  • View blog
  • Posts: 234
  • Joined: 15-November 10

Re: Error. Plural program C++

Posted 31 March 2011 - 05:35 PM

Alright, so you do have to append an `s` to every character, word or just to the end of the array?

This post has been edited by JaKWaC: 31 March 2011 - 05:35 PM

Was This Post Helpful? 1
  • +
  • -

#5 starkhavenprincessrin   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 31-March 11

Re: Error. Plural program C++

Posted 31 March 2011 - 05:38 PM

View PostJaKWaC, on 31 March 2011 - 05:35 PM, said:

Alright, so you do have to append an `s` to every character, word or just to the end of the array?


I need to add an 's' to the end of whatever word the user puts in. I used school because it was the easy.
Was This Post Helpful? 0
  • +
  • -

#6 JaKWaC   User is offline

  • D.I.C Head

Reputation: 76
  • View blog
  • Posts: 234
  • Joined: 15-November 10

Re: Error. Plural program C++

Posted 31 March 2011 - 05:43 PM

Alright, so a c string is a sequential block of memory of char's. Right now your initializing both your arrays to 10. Once you ask the user for the word your first your arrays looks like this:

[s][c][h][o][o][l][\0][0][0][0]
You need to copy your first array to the second array and make it look like this:
[s][c][h][o][o][l][s][\0][0][0]
Was This Post Helpful? 1
  • +
  • -

#7 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1749
  • View blog
  • Posts: 5,901
  • Joined: 03-August 09

Re: Error. Plural program C++

Posted 31 March 2011 - 05:48 PM

the last value in the array is 0(null terminated), so you can place the 's' right there. then you need to make sure that the next value is zero so the string remains null terminated. so that boils down to the following

*find the index of the trailing zero and place 's' there
*add a zero right after the new 's'
Was This Post Helpful? 1
  • +
  • -

#8 starkhavenprincessrin   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 31-March 11

Re: Error. Plural program C++

Posted 31 March 2011 - 05:50 PM

View PostJaKWaC, on 31 March 2011 - 05:43 PM, said:

Alright, so a c string is a sequential block of memory of char's. Right now your initializing both your arrays to 10. Once you ask the user for the word your first your arrays looks like this:

[s][c][h][o][o][l][\0][0][0][0]
You need to copy your first array to the second array and make it look like this:
[s][c][h][o][o][l][s][\0][0][0]


I know that I need it to do that, it's just that I'm not quite sure how to program it. I thought that what I had would do it, but it doesn't. And my book and notes don't have anything like this in it.

How would I do that, just a starter, or psudocode will help.

View Postishkabible, on 31 March 2011 - 05:48 PM, said:

the last value in the array is 0(null terminated), so you can place the 's' right there. then you need to make sure that the next value is zero so the string remains null terminated. so that boils down to the following

*find the index of the trailing zero and place 's' there
*add a zero right after the new 's'


How would that code look? I have nothing to go by or any examples in my book.
Was This Post Helpful? 0
  • +
  • -

#9 JaKWaC   User is offline

  • D.I.C Head

Reputation: 76
  • View blog
  • Posts: 234
  • Joined: 15-November 10

Re: Error. Plural program C++

Posted 31 March 2011 - 05:55 PM

Use a while loop to count the number of elements in the array till you find the null character (\0)
while string[index] not null-character
     increment index
end

Was This Post Helpful? 1
  • +
  • -

#10 starkhavenprincessrin   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 31-March 11

Re: Error. Plural program C++

Posted 31 March 2011 - 05:58 PM

View PostJaKWaC, on 31 March 2011 - 05:55 PM, said:

Use a while loop to count the number of elements in the array till you find the null character (\0)
while string[index] not null-character
     increment index
end


like this?

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


Was This Post Helpful? 0
  • +
  • -

#11 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1749
  • View blog
  • Posts: 5,901
  • Joined: 03-August 09

Re: Error. Plural program C++

Posted 31 March 2011 - 06:05 PM

instead of asking us if it's right, test it ;)
try to ask your self "dose it work? if so or not why?"
Was This Post Helpful? 1
  • +
  • -

#12 starkhavenprincessrin   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 31-March 11

Re: Error. Plural program C++

Posted 31 March 2011 - 06:13 PM

okay so I tested the program and still got the same results as before, I've narrowed down where the code could be bad and I think it's in the piece where I have to copy the orig[] to the plural[], and then add s

        char temp[10];
	for ( int i = 0; i > index; i++)
	{
		temp[i] = orig[i];
		plural[i] = temp[i] + 's';
	}



I figured I'd use a for loop but maybe I'm wrong in that, I'm also not sure if I'm coping and storing the orig[] correctly.
Was This Post Helpful? 0
  • +
  • -

#13 JaKWaC   User is offline

  • D.I.C Head

Reputation: 76
  • View blog
  • Posts: 234
  • Joined: 15-November 10

Re: Error. Plural program C++

Posted 31 March 2011 - 06:16 PM

The loop you just wrote gives you the position of the null character. Now you simple need to replace that single character with an `s`, and the following index (index+1) with a null character (\0). Its two lines of code after you find the position of the null character.
Was This Post Helpful? 1
  • +
  • -

#14 starkhavenprincessrin   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 31-March 11

Re: Error. Plural program C++

Posted 31 March 2011 - 06:33 PM

Well I can get the s at the end of the array now, but it won't show the rest of the word:

Enter word:
school
╠╠╠╠╠╠s
Press any key to continue . . .

the code is now:

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] + 's';
		plural[index+1] = '\0';

	}
}


Was This Post Helpful? 0
  • +
  • -

#15 JaKWaC   User is offline

  • D.I.C Head

Reputation: 76
  • View blog
  • Posts: 234
  • Joined: 15-November 10

Re: Error. Plural program C++

Posted 31 March 2011 - 06:35 PM

Your adding s to every single character in the array.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2