d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Make Me A Better Programmer - From Step 1
Prev1101112131456Next
Add Reply New Topic New Poll
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Sep 2 2012 03:15am
I think I get pointers finally. Strings still a work in progress. This whole idea of C and C++ strings is a bit interesting.....
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Sep 13 2012 04:47pm
my first cs2250 project. A bit more code than those cs1250 projects.

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

void firstDec(string passedstring, int pos);
string inputHandler(string &initial);
int decCheck(string passedstring, int pos);
int decCheck2(string passedstring, int pos);
void secondDec1(string passedstring, int pos);
void secondDec2(string passedstring, int pos);

int main()
{
   string initial, compare, passedstring;
   int dec, pos, dec2;

   getline(cin, initial, 'Q');

   while (initial != compare)
   {
       passedstring = inputHandler(initial);
       pos = int(passedstring.find(" "));
       dec = decCheck(passedstring,pos);
       if(dec == 1)
       {
           firstDec(passedstring, pos);
           cout << "\n";
       }
       else if (dec == 2)
       {
           dec2 = decCheck2(passedstring,pos);
           if (dec2 == 1)
           {
               secondDec1(passedstring, pos);
               cout << "\n";
           }
           else if (dec2 == 2)
           {
               secondDec2(passedstring, pos);
               cout << "\n";
           }
       }


   }

return 0;
}

void firstDec(string passedstring, int pos)
{
   string string1, string2, stem;
   int len, len1;
   len = int(passedstring.length());
   string1 = passedstring.substr(0,pos);
   string2 = passedstring.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;
}

void secondDec1(string passedstring, int pos)
{

   string string1, string2, stem;
   int len, len1;
   len = int(passedstring.length());
   string1 = passedstring.substr(0,pos);
   string2 = passedstring.substr(pos+1,len-pos);
   len1 = int(string2.length());
   stem = string2.substr(0,len1-1);

   cout << "Nom " << string1 << " " <<  string2 << endl;
   cout << "Gen " << stem+"i" << " " << stem+"orum" << endl;
   cout << "Dat " << stem+"o" << " " << stem+"is" << endl;
   cout << "Acc " << stem+"um"<< " " << stem+"os" << endl;
   cout << "Abl " << stem+"o" << " " << stem+"is" << endl;
}

void secondDec2(string passedstring, int pos)
{

   string string1, string2, stem;
   int len, len1;
   len = int(passedstring.length());
   string1 = passedstring.substr(0,pos);
   string2 = passedstring.substr(pos+1,len-pos);
   len1 = int(string2.length());
   stem = string2.substr(0,len1-1);

   cout << "Nom " << string1 << " " <<  string2 << endl;
   cout << "Gen " << stem+"i" << " " << stem+"orum" << endl;
   cout << "Dat " << stem+"o" << " " << stem+"is" << endl;
   cout << "Acc " << stem+"um"<< " " << stem+"a" << endl;
   cout << "Abl " << stem+"o" << " " << stem+"is" << endl;
}

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

int decCheck(string passedstring,int pos)
{
   int fdcheck;
   fdcheck = int(passedstring.find("ae", pos));
   if (fdcheck > 0)
   {
       return 1;
   }
   else return 2;
}

int decCheck2(string passedstring, int pos)
{
   int check1, check2, len;
   string first;
   first = passedstring.substr(0,pos);
   len = int(first.length());
   check1 = int(first.find("r", len-2));
   check2 = int(first.find("us", len-2));
   if (check1 > 0 || check2 > 0)
   {
       return 1;
   }
   else return 2;
}

Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Sep 13 2012 07:59pm
grats

doing it yourself is the only way youre going to move on and learn. hopefully you picked up on my subtle function hints and code snippets in your other thread.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Sep 13 2012 10:15pm
Quote (AbDuCt @ Sep 13 2012 08:59pm)
grats

doing it yourself is the only way youre going to move on and learn. hopefully you picked up on my subtle function hints and code snippets in your other thread.


yeah, I saw that I could have done this one of two ways: purely c or c++. Mixing it would not have been very fun.

my biggest hurdles were the inputHandler function and remembering to loop in main, and also the idea of having an 'empty' string (compare) which I could use to end the loop. Everything worked so well after I had the idea for inputHandler.....(just takes that big ass string, passes one line, deletes it, since its called by reference...works out nice)

This post was edited by Eep on Sep 13 2012 10:17pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Sep 15 2012 03:33am
technically I am writing scripts I guess aren't I, not really programs
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Sep 15 2012 11:03am
Quote (Eep @ Sep 15 2012 05:33am)
technically I am writing scripts I guess aren't I, not really programs


everyone has theyre own definition between the two. i personally define a script as something that doesnt need to be compiled, but some people define it as short pieces of code. tbh it doesnt matter what you call it as long as it works in the end lol.
Member
Posts: 4,541
Joined: Sep 15 2011
Gold: 10,391.00
Sep 16 2012 12:27am
Quote (Eep @ Sep 15 2012 02:33am)
technically I am writing scripts I guess aren't I, not really programs


scripts are generally stateless. they are written to do (or not do) things based on whatever input you give them. a script given the same input will always generate the same output.

among other things, programs maintain their own state. they are written such that the same input can generate different results depending on the state the program is currently in.

edit: yes, I just made this up. so it's likely not entirely accurate, but is a useful distinction.

This post was edited by irimi on Sep 16 2012 12:28am
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Sep 16 2012 04:03am
Quote (irimi @ Sep 16 2012 01:27am)
scripts are generally stateless.  they are written to do (or not do) things based on whatever input you give them.  a script given the same input will always generate the same output.

among other things, programs maintain their own state.  they are written such that the same input can generate different results depending on the state the program is currently in.

edit: yes, I just made this up. so it's likely not entirely accurate, but is a useful distinction.


makes sense to me. I dunno how long it'll be before I actually write something that is more than just a simple system of inputs/outputs.....

Like, I am not really motivated to do stuff like make games etc, but I am hugely interested in how stuff like that works.

Programs with a GUI, programs that draw or do stuff, programs that have to access lots of external files to store info, etc.

Once I get comfortable with stuff like that I would feel pretty confident in making my own program sometime.

oh also encryption/security/databases and shit.....optimization, etc. I would like to be fluent in that stuff as well.

This post was edited by Eep on Sep 16 2012 04:04am
Member
Posts: 30,712
Joined: Sep 19 2007
Gold: 340.00
Sep 16 2012 07:17pm
There's a lot of repeated code in this latest example. Can you think of a way to break it up?
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Sep 16 2012 07:49pm
Quote (J_B @ Sep 16 2012 08:17pm)
There's a lot of repeated code in this latest example. Can you think of a way to break it up?


I could. I suppose I could possibly make the 3 print functions into one if I was creative enough.

Thought I already turned it in ><

next time for sure. I was just so excited after I got it to work that I forgot about cleaning it up ><

Even though I was somewhat familiar with the c++ BASIC basics, I still don't know a whole lot about programming stuff in general so when my professor asked us:

"Take these inputs, do whatever you have to to them, and have it print out these results"

Naturally I thought it would be easy, but when I actually got to sitting down, the biggest hurdle I had was just how to handle the inputs.

I think I might be a little more well versed in that now. Also casting, I never knew you could just do like int(string.length()) and it would cast it as an int.

This post was edited by Eep on Sep 16 2012 07:51pm
Go Back To Programming & Development Topic List
Prev1101112131456Next
Add Reply New Topic New Poll