Lost with Arrays

hello -
I'm lost and trying to write a program that takes in a sentence from the User and converts it to pig latin. That is, remove each first letter of a work and place it at the end of the word. I'm stumped. I can't even get my mind around this and I've been on it for 4 hours. If someone could provide me with some helpful hints, I would great appreciate 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

#include <iostream>
#include <cctype>
#include <string>
#include <cstring>
#include <stdio.h>

using namespace std;

//Function Prototype
	void translateUser(int);    //Function prototype
int main()
	int SIZE = 256;				//Max size of string
	int userInput[256];			//String Input
	int count = 0;				//Counter
	int num = 0;				//counter for pointer conversion

//Get the User's input.
cout << "Please enter a sentence and hit the Return key when complete. \n";
cin.getline(userInput);

//Search the array for "0" or \0  values
StrPtr=&string[0];

	while (*StrPtr!='\0')
	{
		for (count = 0; count < SIZE; count++)
		{
		if (isspace(string))
		//If it's a space, must do something 
		cout << "space encountered n\";

		if ((isalpha(string) || isdigit(string))
		//If a letter occurs, move on
		cout << count[] << endl;
			}
			return 0;
		}
} 
Hi there,

Looks like you're missing an opening brace of your main() function.
Now, you would probably be best of using std::string over c-strings.

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

std::string input, tmp;

//Get the User's input.
cout << "Please enter a sentence and hit the Return key when complete. \n";
cin.getline(input);

std::istringstream iss(input); //temporary stringstream

while (iss >> tmp)  //while there are words in the sentence
{
    char first = tmp[0]; //get first character
    tmp.erase(0,1); //erase first character
    tmp.append(first); //put first character at the back

    std::cout << tmp;
}


That should get you on your way, please do let us know if you require any further help.

All the best,
NwN
Not sure why you have an array of ints on line 14 I am going to assume you wanted an array of char?

But since you included string in your header I would start approaching the problem like this.


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

using namespace std;


//Function Prototype
void translateUser(string&);    //Function prototype


int main(){

	string userInput;			//String Input

	//Get the User's input.
	cout << "Please enter a sentence and hit the Return key when complete." << endl;
	getline(cin, userInput);

        //Finish Up
	translateUser(userInput);

	return 0;

}

void translateUser(string &userInput){

	//Function Here

}
I obviously don't know what I am doing. I was told I need to use the C++ string class. Even a more difficult understanding now for me....
Thank you for your suggestions and time
The suggestions from NwN and mobotus do use the C++ string class.

What specific question(s) do you have now?

Last edited on
Look over the string class here http://www.cplusplus.com/reference/string/

Here is some code to look over.

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

using namespace std;


int main(){

	string tmp = "cat";
	string end = "t";

	char first = tmp.at(0); //get first character
	tmp.erase(0, 1); //erase first character
	tmp += first; //append char first to string tmp
	tmp.append(end); //append additional content to string

	cout << tmp; //cat is now atct

	return 0;

}
Last edited on
Topic archived. No new replies allowed.