this is all that is given:

char word[10];
addS("fasten", word);
cout << endl << word << endl;  // will output fastens

for a program that must make a word plural (add an s)

here is my code, which compiles but gives a peculiar "segmentation fault" that I haven't seen before..

void addS(char [], char []);

int main() {

    char word[10];
    addS("fasten", word);
    cout << endl << word << endl;  // will output fastens    

    return 0;

} // main

void addS(char orig[], char plural[]) {
    
    int i;
    for(i = 0; true ; i++)
        plural[i] = orig[i];
        
    plural[i + 1] = 's';
    plural[i + 2] = '\0';

}// addS

I'm really lost on this. plz help.

how about this, it gives "segmentation fault"

void addS(char [], char []);

int main() {
    
    char word[7]; 
    char singular[6];
    cout << "Input 6-letter word to make plural: ";
    cin >> singular;
    cout << singular << endl;
    addS(singular, word);
    cout << endl << word << endl;   

    return 0;

} // main

void addS(char orig[], char plural[]) {
    
    int i;
    for(i = 0; orig[i] != '\0' ; i++)
        plural[i] = orig[i];
    
    plural[i + 1] = 's';

}// addS

1. You're not leaving space for the '\0' terminator.
2. This line is wrong:

plural[i + 1] = 's';

3. The user could still enter more than 6 letters and cause a buffer overrun.

I'd recommend using a std::string instead. Or at least use getline to limit the number of characters entered.

I'm not allowed to use anything besides char [] for this course. thx, i was just looking at that line and thought it should be plural = 's' but that doesn't work either...

yes! thanks! this works..

void addS(char [], char []);

int main() {
    
    char word[8]; 
    char singular[7];
    cout << "Input 6-letter word to make plural: ";
    cin >> singular;
    singular[7] = '\0';
    addS(singular, word);
    cout << endl << word << endl;  // will output schools    

    return 0;

} // main

void addS(char orig[], char plural[]) {
    
    int i;
    for(i = 0; orig[i] != '\0' ; i++)
        plural[i] = orig[i];
    
[B]    plural[i] = 's';
    plural[i + 1] = '\0';[/B]

}// addS

note that i changed it so it's not beyond the array bounds. it's working now, thx

Taken from MSDN:

sizeof Operator  


Yields the size of its operand with respect to the size of type char.

Wouldn't this be usefull? It would probably save you a loop. It's also usefull to check if the user entered a 6-digit word.

Anyway, you made it, congratulations.

Greetz, Eddy

Wouldn't this be usefull? It would probably save you a loop. It's also usefull to check if the user entered a 6-digit word.

I think you need to look at strlen .

I think you need to look at strlen .

I don't think that really matters. strlen is for strings only I guess, sizeof() can give the size of multiple variable types (Never really tried which types).

Anyway, he's got it working.


@the thread starter, to expand your program and learn a bit more you might also want to add support for words with more characters and don't add an "s" if the last character (before the /0) already is "s".

Greetz, Eddy

How do you think sizeof would save a loop, or check the length of an entered string? String length may be different from array size.

You're right. sizeof() returns the array size, not the amount of characters in an array, and strlen only works for strings. You could of course convert the char to a string, but that's not the point here.

#include "stdafx.h"
#include <iostream>

using namespace std;

int main(int argc, char* argv[])
{
	char singular[6];
	cout << "Insert a word with max 6 characters to make plural: ";
	cin >> singular;
	char plural[7];
	sprintf(plural, "%ss", singular);
	cout << "The plural of " << singular << " is: " << plural << endl;
	return 0;
}

works fine too...

WARNING: INSERTING MORE THEN 6 CHARACTERS WILL RESULT IN A CRASH!


Greetz, Eddy

This question has already been answered. Start a new discussion instead.