char array

i have 1 input array of chars 20,30,+ and i want to store it in 2d arr

Page 1 of 1

3 Replies - 915 Views - Last Post: 12 January 2010 - 05:27 AM Rate Topic: -----

#1 rafaqat hassan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 09-January 10

char array

Posted 10 January 2010 - 12:45 AM

#include<iostream.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#define x 50
void main ()
{
char post[x]={'2','0',',','3','0',',','+',',','#'};
char postfix[x][10];
int l,d=0,i=0;
l=strlen(post);
while (post[d]!='#')
{int k=0;
while(post[d]!=',')
{postfix[i][k]=post[d];
k++;
d++;}
i++;d++;
}
for (int r=0;r<d;r++)
cout<<postfix[r];
getch();
}



** Edit ** :code:

Is This A Good Question/Topic? 0
  • +

Replies To: char array

#2 no2pencil  Icon User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6564
  • View blog
  • Posts: 30,701
  • Joined: 10-May 07

Re: char array

Posted 10 January 2010 - 12:46 AM

Void main == bad
Lack of indenting == bad

Also, please post your question in the body of the post, & not in the subject. Thanks.
Was This Post Helpful? 0
  • +
  • -

#3 anonymouscodder  Icon User is offline

  • member icon

Reputation: 126
  • View blog
  • Posts: 710
  • Joined: 01-January 10

Re: char array

Posted 10 January 2010 - 04:49 AM

for (int r=0;r<d;r++)
cout<<postfix[r];

You will got garbage while printing this.

What are you trying to do? Your code is unclear.

This post has been edited by anonymouscodder: 10 January 2010 - 09:30 AM

Was This Post Helpful? 0
  • +
  • -

#4 rafaqat hassan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 09-January 10

Re: char array

Posted 12 January 2010 - 05:27 AM

actually i am trying this
i am take i array of char whis is 20,30,+ and i want to convert it to other array which is 2d array in 1st location it store 20 ignore "," 2nd location it store 30 than ignore "," than 3rd it store + i am doing it for postfix evaluation .i am sending that code too in this coad m having problem that it is working for 1digit but for two digits it is giving garbig value

#include <stdio.h>
#include<iostream.h>
#include <conio.h>
#include<string.h>
#define MAX 50
char stack[MAX];
char postfix[MAX];
int top=-1;
char pop();
void push(char);
int Evaluate(void);
int power(int b,int a)
{if (a==0)
return 1;
return b*power(b,a-1);}
void main()
{
int m;
clrscr();
cout<<"enter postfix equation: ";
gets(postfix);
m=Evaluate();
cout<<m;
getch();
}
int Evaluate(void)
{
int i,l,a,b,q,z;
l=strlen(postfix);
for(i=0;i<l;i++)
{
if (postfix[i] != '^' &&postfix[i] != '*' &&postfix[i] != '+' &&postfix[i] != '/' &&postfix[i] != '-' )
{
push(postfix[i]);
}
else if(postfix[i] == '^' ||postfix[i] == '*' ||postfix[i] == '+' ||postfix[i] == '/' ||postfix[i] == '-' )
{
a = pop ( );
b = pop ( );
a-=48;
b-=48;

switch(postfix[i])
{
case '^' : q=power(b,a); break;
case '+' : q=b+a; break;
case '-' : q=b-a; break;
case '*' : q=b*a; break;
case '/' : q=b/a; break;
}
push ( q );
}
}
z = pop ();
return z;
}
void push(char item)
{
top++;
stack[top]=item;
}
char pop()
{
char a;
a=stack[top];
top--;
return a;
}


*** MOD EDIT: Added code tags. Please :code: ***
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1