d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > My 2nd Project, Just Need Some Advice
Prev123Next
Add Reply New Topic New Poll
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Sep 29 2012 08:43pm
god I can't figure this out

so here is a snippet of test code I made


Code
int main() {

char temp[20];
char cmp[] = "'\n'";

while(cin.getline(temp,19,' ')){
cout << temp << endl;
}
return 0;
}


when given this input
Code
2 4 6 8 10 12
14 16 18 0 1


it prints out


Code
2
4
6
8
10
12
14
16
18
0
1




there are 2 newlines at the bottom which I obviously don't want. I don't know how to get rid of them.

This post was edited by Eep on Sep 29 2012 08:43pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Sep 30 2012 12:38am
Quote (Eep @ Sep 29 2012 10:43pm)


that code doesnt make sense lol. what are you trying to do? the cmp string isnt being used and i cant see how you are getting that output from that code lol

if you want to split a string try something like this

im also pretty sure there is a split method within the STRING class

Code
#include <stdio.h>
#include <stdlib.h>

//need to include this for the split function
#include <string.h>

void split(char *input, char *dilimeter, char **output);


int main()
{
   char userInput[81];
   char *functionOutput[80];
   int i = 0;


   printf("Enter a string to test the split function\n");
   fgets(userInput, sizeof(userInput), stdin);

   split(userInput, ".", functionOutput);int i

   while(functionOutput[i] != '\0')
   {
       printf("%s\n", functionOutput[i]);
       i++;
   }

   getchar();
   return 0;

}



void split(char *input, char *dilimeter, char **output)
{
   int i = 0;

   output[0] = strtok(input, dilimeter);

   while(output[i] != NULL)
   {
       i++;
       output[i] = strtok(NULL, dilimeter);
   }

   output[i + 1] = '\0';
}


edit:: i could of changed it to return a char * so that i wouldnt need to include a **output buffer.

would also make the call to the function cleaner by way of

Code
output = split(input,' ');


This post was edited by AbDuCt on Sep 30 2012 12:43am
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Sep 30 2012 02:00am
I mean split seems a bit complicated for what I need


All I need to do is read a list of inputs, like

Code
2 4 * 3 - p
5 2 4 + * p
5 u 4 - 3 / p
5 4 - u 2 * p
7 u 12 - u 3 4 - * 7 / p e


and do operations based on what it is. I need to input the characters into a c-string so I can use atoi on them. I need to delim by the white spaces, but then I have to worry about the newlines and I can't figure out a way to deal w/ both at the same time.

Like literally just read in one c-string at a time. (has to be string for stuff like double digit numbers)

if its a digit, atoi and push it.

if its an operator, perform the operation.

This post was edited by Eep on Sep 30 2012 02:01am
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Sep 30 2012 10:39am
try something like this

Code
int main()
{
   string str = "0x0002,A5651QPR87GBZ094RTF52,D,A,000001,ABC ,10000.00 , EOT";
   string word;
   stringstream stream(str);
   while( getline(stream, word, ',') )
       cout << word << "\n";
}
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Oct 3 2012 01:06am
okay so I was dumb and never considered the fact that good ol' CIN works just fine


anyways, I want to make my project look a bit nicer now


Can I possibly condense this logic statement?

Code
if (temp[0] == '+' || temp[0] == '-' || temp[0] == '*' || temp[0] == '/')


This post was edited by Eep on Oct 3 2012 01:06am
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Oct 3 2012 01:20am
Finished product (reverse polish calculator)

Code
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;

struct stack
{
   int value;
   stack *next;
};

stack *calcStack = NULL;

void push(int num);
int pop();
void operation(char op);
void operation2(char op);
bool opCheck(char op);
int main()
{
   char temp[20];
   while(cin >> temp)
   {
       if (isdigit(temp[0]))
       {
           push(atoi(temp));
       }
       else if (opCheck(temp[0]))
       {
           operation(temp[0]);
       }
       else
       {
           operation2(temp[0]);
       }
   }
return 0;
}

void push(int num)
{
   stack *newItem = new(stack);
   newItem->value = num;
   newItem->next = calcStack;
   calcStack = newItem;
}

int pop()
{
   int item = calcStack->value;
   stack *tempPtr = calcStack;
   calcStack = calcStack->next;
   delete tempPtr;
   return item;
}

void operation(char op)
{
   int num1, num2;
   num1 = pop();
   num2 = pop();

   switch(op)
   {
       case '+':
           push(num2+num1);
           break;
       case '-':
           push(num2-num1);
           break;
       case '*':
           push(num2*num1);
           break;
       case '/':
           push(num2/num1);
           break;
       default:
           break;
   }
}

void operation2(char op)
{
   int num;
   switch(op)
   {
       case 'u':
           num = pop();
           push(-num);
           break;
       case 'p':
           num = pop();
           cout << num << endl;
           break;
       case 'e':
           cout << "Thanks for using the reverse polish calculator." << endl;
           exit(0);
       default:
           break;
   }
}

bool opCheck(char op)
{
   if (op == '+' || op == '-' || op == '*' || op == '/')
   {
       return true;
   }
   return false;
}


just made a function to reduce clutter in main :/
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Oct 3 2012 07:48am
Quote (Eep @ Oct 3 2012 03:06am)
okay so I was dumb and never considered the fact that good ol' CIN works just fine


anyways, I want to make my project look a bit nicer now


Can I possibly condense this logic statement?

Code
if (temp[0] == '+' || temp[0] == '-' || temp[0] == '*' || temp[0] == '/')


your not using this in your new code but anyways to make it look a bit cleaner iirc temp always points to temp[0] so you could of wrote it like

Code
if (temp == '+' || temp == '-' || temp == '*' || temp == '/')


although i like the first one because it makes more sense when youre reading it.

This post was edited by AbDuCt on Oct 3 2012 07:49am
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Oct 3 2012 09:42am
Quote (AbDuCt @ Oct 3 2012 08:48am)
your not using this in your new code but anyways to make it look a bit cleaner iirc temp always points to temp[0] so you could of wrote it like

Code
if (temp == '+' || temp == '-' || temp == '*' || temp == '/')


although i like the first one because it makes more sense when youre reading it.


Technically it is in the new code just as a function (where temp [0] was passed and became char op))
Member
Posts: 13
Joined: Oct 23 2003
Gold: 0.00
Oct 5 2012 07:43pm
Quote (Eep @ Sep 29 2012 08:20am)
Should I create separate functions for every operator? Seems like it would be a bit bulky that way, but I like the organization aspect of it.

I haven't begun writing yet as I want to kind of think about how to tackle this from a pseudo stand point....it is by no means a hard project, but I want to know when I should/shouldn't use lots of functions


Instead of creating a function for every operator you could implement one template function.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Oct 5 2012 08:35pm
Quote (boobalot @ Oct 5 2012 08:43pm)
Instead of creating a function for every operator you could implement one template function.


I think we are learning about templates next. Haven't quite got to it yet.
Go Back To Programming & Development Topic List
Prev123Next
Add Reply New Topic New Poll