d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Seeking Wisdom For My First Project! Help Please!
Prev123Next
Add Reply New Topic New Poll
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Sep 13 2012 02:00pm
I'll keep you up to date on anything I found

Forgot if you mentioned this or not but I ended up using the std::string getline to fix the input problem

now I use this:

Code
getline(cin, initial, 'Q');


initial being my string

so, now I have a giant string which looks like

abc 123
def 456
ghi 789

etc etc

two words separated by space then a return

Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Sep 13 2012 02:33pm
Got one of my functions to work (just need to beautify the output...)
Code
void firstDec(string initial, int pos)
{
   string string1, string2, stem;
   int len, len1;
   len = int(initial.length());
   string1 = initial.substr(0,pos);
   string2 = initial.substr(pos+1,len-pos);
   len1 = int(string2.length());
   stem = string2.substr(0,len1-2);

   cout << "Nom " << string1 << string2 << endl;
   cout << "Gen " << stem+"ae" << stem+"arum" << endl;
   cout << "Dat " << stem+"ae" << stem+"is" << endl;
   cout << "Acc " << stem+"am" << stem+"as" << endl;
   cout << "Abl " << stem+"a" << stem+"is" << endl;
}


Now I am still tackling a new problem:

Now that I have a giant string of words, is there a way for me to like:

(1) Read a line of the string up until a RETURN charcter
(2) Create a sub string of that line
(3) Delete that line from the initial string
(4) Pass the substring to some function which checks to see which declination its in
(5) From there have that function pass the string to the proper declination function and print out the results
(6) repeat the process again until there is no longer any lines to read in the initial string

??

Code
string inputHandler(string initial)
{
   string passedstring;
   int pos, strlen;
   strlen = int(initial.length());
   pos = int(initial.find('\n'));
   passedstring = initial.substr(0,pos);
   initial.erase(0,pos+1);
   return passedstring;
}


this seems to work, but now I need a way for it to loop. Everytime I add a loop, the program doesn't do any of the operations.

IE, if I had a while statement before the line with (pos = int blah blah)

I would run the program, enter in my array, pass it to that function, and it wouldn't do anything.

This post was edited by Eep on Sep 13 2012 03:01pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Sep 13 2012 03:09pm
thinking back on it, I guess it wouldn't make sense for a function itself to loop returns.

Instead, I should create a loop in the main function that calls to inputHandler

anyone got some tips? lol

edit; making headway!!

Code
#include <iostream>
#include <string>
using namespace std;

void firstDec(string initial, int pos);
string inputHandler(string &initial);

int main()
{
   string initial, compare, passedstring;

   getline(cin, initial, 'Q');

   while (initial != compare)
   {
       passedstring = inputHandler(initial);
   }

return 0;
}

void firstDec(string initial, int pos)
{
   string string1, string2, stem;
   int len, len1;
   len = int(initial.length());
   string1 = initial.substr(0,pos);
   string2 = initial.substr(pos+1,len-pos);
   len1 = int(string2.length());
   stem = string2.substr(0,len1-2);

   cout << "Nom " << string1 << string2 << endl;
   cout << "Gen " << stem+"ae" << stem+"arum" << endl;
   cout << "Dat " << stem+"ae" << stem+"is" << endl;
   cout << "Acc " << stem+"am" << stem+"as" << endl;
   cout << "Abl " << stem+"a" << stem+"is" << endl;
}

string inputHandler(string &initial)
{
   string passedstring, compare;
   int pos;
   pos = int(initial.find('\n'));
   passedstring = initial.substr(0,pos);
   initial.erase(0,pos+1);
   return passedstring;
}


works how I want it so far....

This post was edited by Eep on Sep 13 2012 03:19pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Sep 13 2012 04:45pm
finished on my own

feels better that way I guess.
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Sep 13 2012 07:56pm
np
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Sep 13 2012 10:20pm
Quote (AbDuCt @ Sep 13 2012 08:56pm)
np


thanks for the tips. I just really needed to bounce ideas and also think things through systematically a bit.

Why would it ever make sense for a function to loop returns.....dunno HOW I thought that would ever work...
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Sep 14 2012 07:55am
Quote (Eep @ Sep 14 2012 12:20am)
thanks for the tips. I just really needed to bounce ideas and also think things through systematically a bit.

Why would it ever make sense for a function to loop returns.....dunno HOW I thought that would ever work...


lol

happens to the best of us at times. also bouncing ideas is great i do it all the time on irc with my other programming buddies. helps you learn as well as get other views on a specific task you are trying to complete.
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Sep 14 2012 12:29pm
Quote (Eep @ Sep 13 2012 09:20pm)
thanks for the tips. I just really needed to bounce ideas and also think things through systematically a bit.

Why would it ever make sense for a function to loop returns.....dunno HOW I thought that would ever work...


one reason might be because you're trying to take shortcuts in thinking rather than doing the actual work of simulating the code execution, line by line, in your head. at some point, you might be able to read & write larger programs without having to go through it line by line, but in the meantime it's a skill that you MUST develop in order to get better.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Sep 14 2012 02:36pm
Quote (irimi @ Sep 14 2012 01:29pm)
one reason might be because you're trying to take shortcuts in thinking rather than doing the actual work of simulating the code execution, line by line, in your head.  at some point, you might be able to read & write larger programs without having to go through it line by line, but in the meantime it's a skill that you MUST develop in order to get better.


you're right. By the time I had started, I was doing the wrong functions.

I should have done like

inputHandler --> decChecker

but instead (after I had input) I wrote the first dec function, even though I had no inputs to give it yet!

BUT, I have started to slowly get a better style of

Write something small, test it, tests its functionality with everything else, then go to the next piece. This assignment REALLY drove that in to me

example: After I finished the 2 big functions, I made sure each one worked by simple cout's.

I would check to see where the program 'was' during certain parts of execution (like putting a cout after a line in a function to see what a variable was equal to, etc)

I am SLOWLY starting to make better habits, but it is still a giant work in progress. I haven't quite got to the point where I can think of the code executing line by line on a grander scale of more than like 3-4 lines lol. If I think of any more than that at a time its rough at the moment.
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Sep 14 2012 03:18pm
Quote (Eep @ Sep 14 2012 01:36pm)
I would check to see where the program 'was' during certain parts of execution (like putting a cout after a line in a function to see what a variable was equal to, etc)


This is important and useful too, so I'm not trying to downplay the significance of understanding a program's state between lines of code, as it's probably the most-oft employed debugging technique in the field.

But understanding program state can only be helpful if you already have a solid grasp of control flow in a program (yes, there's that phrase again) --- and that's really the point of the assignment anyway. Instead of trying to figure out whether you did the right thing by trial-and-error ("if I put this input, does the output look like that?"), you really ought to be figuring out how the program is actually running ("what actually happened to cause the input to transform to this output?"). Without this foundation of understanding of simply how a program works, you'll never really "get it" even as you move on to more advanced material.

Not that I'm blaming you for not doing this -- your teachers may not be emphasizing it enough for all I know. I'll be the first to admit that it's only with the gift of hindsight that I could look at your assignment and say, "This is what you're supposed to learn here."

This is also one of the reasons why compiled languages (like C++ and Java) kind of make shitty languages to start learning programming with. Interpreted languages like LISP & Python force you to pay attention to those things first and foremost, rather than trivial/stupid stuff like syntax.

This post was edited by irimi on Sep 14 2012 03:21pm
Go Back To Programming & Development Topic List
Prev123Next
Add Reply New Topic New Poll