Any C++ programmers here? Help Needed...

Renkz

Superstar
Supporter
Joined
Jun 12, 2012
Messages
7,881
Reputation
2,400
Daps
18,234
Reppin
NULL
Code:
void processLine(string _line)
{
    string buffer;
    string outPutLine;

    int lineLen = _line.length();


    for (int i = 0; i < (lineLen); i++)
    {

        if (isspace(_line[i]) || i == (lineLen - 1) || _line[i] == ',' || _line[i] == '.' || _line[i] == '-')// all of our delimiters
        {
            string temp;
            bool lastCharInLineFlag = false;
            bool commaOrPeriodAsLastCharInLineFlag = false;

            //check if we are on the last char of the line;
            if (i == (lineLen - 1))
            {
                lastCharInLineFlag = true;

                //check if the last char is a . or , ...
                //if not, add the last char to buffer to get the complete word.
                //reason for this check is to make sure the code doesn't process the period or comma as part of the word
                if (_line[i] == '.' || _line[i] == ',')
                {
                    commaOrPeriodAsLastCharInLineFlag = true;
                }
                else
                {
                    buffer = buffer + _line[i];
                }
            }



            //add all the chars after the 1st char
            for (int j = 1; j < (int)buffer.length(); j++)
            {
                temp = temp + buffer[j];
            }


            if (buffer.length() > 0)
            {
                //add the first 2 chars from the buffer to the end of temp
                temp = temp + buffer[0];

                //add a and y
                temp = temp + "a";
                temp = temp + "y";
          }
        

            //attach temp to outPutLine
            if (lastCharInLineFlag)
            {
                if (!commaOrPeriodAsLastCharInLineFlag)
                {
                    outPutLine = outPutLine + temp;
                }
                else
                {
                    outPutLine = outPutLine + temp + _line[i];// here is where the code adds the period or comma
                }
            }
            else
            {
                if (_line[i] == ',' || _line[i] == '.')
                {
                    //add period or comma, If the next char is a whitespace, add it to output.
                    //I did this to catch cases when the next char is == to a '-' which occurs in the file
                    outPutLine = outPutLine + temp + _line[i];

                    if (_line[i + 1] == ' ')
                    {
                        outPutLine = outPutLine + ' ';
                        i = i + 1;
                    }
            
                }
                else if (_line[i] == '-')
                {
                    outPutLine = outPutLine + temp + _line[i] + _line[i+1];
                    i = i + 1;
                }
                else
                {
                    outPutLine = outPutLine + temp + _line[i];
                }
            }

            //reset buffer
            buffer = "";
        

        }
        else
        {
            //add chars to buffer untill we hit one of our delimiters
            buffer = buffer + _line[i];
        }
    
    }

    // write line to output
    outputLine(outPutLine);
}



Got home and decided to see if my programming/problem solving skills are still on point!

Yeah, they still crisp.
How long did it take you?
 

Scholar

Superstar
Joined
Dec 11, 2015
Messages
8,939
Reputation
795
Daps
24,347
:francis:
Damn dude don't want to give you the answer here honestly.

However I'll give you a quick logic breakdown to get your brain ticking at least. Any loops and things you need to create and formatting you have to figure out your own:

  • Add the appropriate libraries for the stringstream and string. <sstream> and <string>
  • Create some string variables. One to hold the line of text that you'll read in with while(getline(infile,buffer)) with buffer being the possible name of one of your string variables.
  • Buffer will hold a line of text now and the while encapsulation of the getline will run getline on the whole file until end of file is reach.
  • Now with stringstream, you can feed the buffer string into the stringstream object using cin and cout functions. So either buffer>>ss or ss<<buffer.
  • The beauty of a stringstream object at this point is that it can parse out a line of text word by word. you'll want another string object at this point like called temp or something to feed each individual line of text to be manipulated. You do this with the cout function such as ss>>temp. This will put one word at a time in order into the temp variable. You'll erase the first character of the string, add it to the end, and then also add ay at the end. This needs to be some sort of loop so that it can feed each word of the line into temp until the ss object is completely through the line.
  • One last important thing. in the loop for get line, make sure to do ss.clear() before returning back to the top of the while(getline()) loop. This clears the stringstream buffer and allows it to be filled with new text. Otherwise it will only change one word and it will be stuck

Thats about as much as I'm willing to do for you without doing the work for you. Hopefully this helps and I recommend using stackoverflow and the official c++ website as much as possible.

cplusplus.com - The C++ Resources Network
Stack Overflow
Read up to the bold and stopped. Didn't know if you were trolling or just the goat. Big ups for helping him. Have you used SAS by any chance?
 

AJaRuleStan

All Star
Joined
Feb 23, 2015
Messages
2,466
Reputation
-2,565
Daps
5,482
Reppin
Killa Queens
How long did it take you?
At first, 10 - 15 mins, but when I ran it, I noticed that I completely overlooked all the special characters that were in the file(Period, Comma, and double dash). So I had to go back and add in logic that would catch these special cases and handle them appropriately to produce the proper output. That took around 5 mins. (Note, I didn't confirm if my logic properly process a case where "-" is the last char of a line because that case didn't come up, so my code can only be guaranteed to work with the specific input that the OP provided).

Also, my advice to OP would be to focus on the problem solving process before fixating over the details. Think in terms of what steps do I need to take to get from state A to state B, that's pretty much how I approach programming. If i'm using a more object oriented approach i'll think about the problem in a more abstract manner. In this case, I would imagine some sort of word translator device and think about how it should be used.
 
Last edited:

KritNC

All Star
Joined
Jun 5, 2012
Messages
3,440
Reputation
610
Daps
4,086
Reppin
Costa Rica
Ruby:

Code:
require 'pry'

words = "Four score and seven years ago our fathers brought forth on this
continent a new nation, conceived in liberty, and dedicated to the
proposition that all men are created equal. Now we are engaged in a
great civil war, testing whether that nation, or any nation so conceived
and so dedicated, can long endure. We are met on a great battlefield of
that war. We have come to dedicate a portion of that field, as a final
resting place for those who here gave their lives that that nation might
live. It is altogether fitting and proper that we should do this. But,
in a larger sense, we cannot dedicate--we cannot consecrate--we cannot
hallow this ground. The brave men, living and dead, who struggled here,
have consecrated it, far above our poor power to add or detract. The
world will little note, nor long remember, what we say here but it can
never forget what they did here. It is for us the living, rather to be
dedicated here to the unfinished work which they who fought here have
thus far so nobly advanced. It is rather for us to be here dedicated to
the great task remaining before us,--that from these honored dead we
take increased devotion to that cause for which they gave the last full
measure of devotion--that we here highly resolve that these dead shall
not have died in vain--that this nation, under God, shall have a new
birth of freedom--that government of the people, by the people, for the
people, shall not perish from the earth."

words = words.split(" ")
pig_latin = []
words.each do |word|
  x = "#{word[1..-1]}#{word[0]}ay"
  x.capitalize
  pig_latin.push x
end
puts pig_latin.join(" ")
 

KritNC

All Star
Joined
Jun 5, 2012
Messages
3,440
Reputation
610
Daps
4,086
Reppin
Costa Rica
JS Bin

my js solution.

Code:
var ll = function(sent){
  var pb = []
  var arr = sent.split(' ');
  for(i=0;i<arr.length;i++){
    pb.push(arr[i].substring(1) + arr[i].substring(0,1) + 'ay');
  }
  console.log(pb.join(" ", ','));
  document.write(pb.join(" ", ','))
}

ll("welcome to the coli")


:salute: Same logic I used in ruby. I shoulda used map instead but something was fukking up and I was lazy
 

AJaRuleStan

All Star
Joined
Feb 23, 2015
Messages
2,466
Reputation
-2,565
Daps
5,482
Reppin
Killa Queens
Wait, a lot of ppl are using substring type of methods in their code, aren't you going to run into problems when you process words that contain one character like the letter "a"? I avoided using substring myself because I wasn't sure if I would get an out of bound type of crash in c++ if I tried to index the second position on a 1 element string.

EDIT

I ran a test substring'n the second pos of a 1 element string and the method just returns nothing, so i assume this is the case for other languages too.
 
Last edited:

FSP

Banned
Joined
Jun 19, 2015
Messages
14,284
Reputation
1,108
Daps
42,300
Wait, a lot of ppl are using substring type of methods in their code, aren't you going to run into problems when you process words that contain one character like the letter "a"? I avoided using substring myself because I wasn't sure if I would get an out of bound type of crash in c++ if I tried to index the second position on a 1 element string.
What's "a" in pig latin?
 
Top