7 Replies - 1318 Views - Last Post: 13 February 2012 - 12:28 PM Rate Topic: -----

#1 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Pig Latin Help

Posted 10 February 2012 - 01:59 AM

So I'm new to C++, so I can't quite understand what I'm doing wrong here, but I need to write a program that converts a sentence into pig latin and then prints it out. I understand conceptually what I'm doing here...delineate the sentence at the spaces and store each word in an array. Then modify each spot in the array and iterate through it to print. I think that's what I tried to translate into my code, but it's not working quite right.

#include <cstdlib>
#include <string>
#include <iostream>

using namespace std;

string toPLatin(string);
string printArray(string[], int);

int main()
{
	const int SIZE = 500;
	string orig;
	cout << "Enter sentence to convert to Pig Latin:   ";
	getline(cin, orig);

	string subs[SIZE];

	for(int i = 0; i < SIZE; i++)
	{
		int pos = orig.find(' ');
		if(pos < orig.length()) subs[i] = toPLatin(orig.substr(0, pos));
		else subs[i] = toPLatin(orig);
		orig = orig.substr(pos, orig.length());
	}

	cout << printArray(subs, SIZE);
}

string toPLatin(string str)
{
	string a = str.substr(0,1);
	string newStr = str.substr(1, str.length());
	return newStr + a + "ay";
}

string printArray(string strs[], int size)
{
	string output;
	for(int i = 0; i < size; i++) output += strs[i] + " ";
	return output;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Pig Latin Help

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,517
  • Joined: 23-August 08

Re: Pig Latin Help

Posted 10 February 2012 - 04:54 AM

"It Doesn't Work." That's Not Good Enough
Was This Post Helpful? 0
  • +
  • -

#3 #define  Icon User is online

  • Duke of Err
  • member icon

Reputation: 1345
  • View blog
  • Posts: 4,632
  • Joined: 19-February 09

Re: Pig Latin Help

Posted 10 February 2012 - 03:11 PM

Ouyay reaay teratingiay veroay hetay holeway rrayaay, hereasway ouyay nlyoay antway otay ooplay ntiluay heretay siay onay oremay ordsway niay hetay nputiay tringsay (ndeay foay hetay tringsay).

Spoiler


19	    for(int i = 0; i < SIZE; i++)


Was This Post Helpful? 0
  • +
  • -

#4 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Pig Latin Help

Posted 12 February 2012 - 09:06 PM

View Post#define, on 10 February 2012 - 02:11 PM, said:

Ouyay reaay teratingiay veroay hetay holeway rrayaay, hereasway ouyay nlyoay antway otay ooplay ntiluay heretay siay onay oremay ordsway niay hetay nputiay tringsay (ndeay foay hetay tringsay).

Spoiler


19	    for(int i = 0; i < SIZE; i++)


Ok , so I COMPLETELY understand what you're trying to say and what I need to do, but I don't understand how to make it work. I tried to counting whitespaces and making the length of the string array set to that number, but it's not quite working. I'm not sure why. I'm sorry I don't have much more to go on than that I don't understand.
Was This Post Helpful? 0
  • +
  • -

#5 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Pig Latin Help

Posted 12 February 2012 - 09:40 PM

You can't just keep staring at the code and trying random "fixes" without first seeing what's wrong. You have to learn how to debug.

Suppose we take your code and see what it's doing when it runs ...
#include <cstdlib>
#include <string>
#include <iostream>

using namespace std;

string toPLatin(string);
string printArray(string[], int);


// Suppose we take your code and see what it's doing ...
int main()
{
	const int SIZE = 500;
	string orig;
	cout << "Enter sentence to convert to Pig Latin:   ";
	getline(cin, orig);

	string subs[SIZE];

	for(int i = 0; i < SIZE; i++)
	{
		int pos = orig.find(' ');
        if( pos < orig.length() ) {
            subs[i] = toPLatin(orig.substr(0, pos));
// here:
            cout << subs[i] << endl;
        }
		else {
            subs[i] = toPLatin(orig);
// and here:
            cout << subs[i] << endl;
        }
		orig = orig.substr(pos, orig.length());
// and here:
        cout << orig << endl;
	}

	cout << printArray(subs, SIZE);
}

string toPLatin(string str)
{
	string a = str.substr(0,1);
	string newStr = str.substr(1, str.length());
	return newStr + a + "ay";
}

string printArray(string strs[], int size)
{
	string output;
	for(int i = 0; i < size; i++) output += strs[i] + " ";
	return output;
}



See if that tells you anything.

Do you see anything interesting in the output (before it crashes)?

This post has been edited by r.stiltskin: 12 February 2012 - 09:55 PM

Was This Post Helpful? 0
  • +
  • -

#6 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Pig Latin Help

Posted 13 February 2012 - 11:56 AM

I don't get any output. It just crashes. I don't know where the error is because I don't get a debug report or whatever.

This post has been edited by JackOfAllTrades: 13 February 2012 - 12:20 PM
Reason for edit:: Removed unnecessary quote

Was This Post Helpful? 0
  • +
  • -

#7 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Pig Latin Help

Posted 13 February 2012 - 12:09 PM

Please look closer. When I run it I get output -- both in Linux with G++ and in Windows (MSVC++).

(A little bit of output, and then it crashes.)
Was This Post Helpful? 0
  • +
  • -

#8 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Pig Latin Help

Posted 13 February 2012 - 12:28 PM

I hope you noticed that I added a few cout statements to your code ...

Even when your code crashes with no output, there are things you can do to debug it. Either run it in a debugger so you can see exactly where (at which line of code) it stops. Or put cout statements (or printf in C) at various places in the program to find the last place you can get it to print something before crashing.

Then at least you have a better idea of where to look for errors.

One more thing:
In this case, not only where it crashes, but the content of the output itself is an important clue to what's going wrong.

This post has been edited by r.stiltskin: 13 February 2012 - 02:13 PM

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1