Solved

Converting from a string to char

Posted on 2004-04-27
15
380 Views
Last Modified: 2010-04-02
Basically im trying to make a small calculator...

arguments would be as follows on the command line

calculator.out 3 + 6
3 arguments are passed

i have no problem with the 3 or 6

i need to convert the second arguement from a string of length 1 to a char and assign it to a variable.

Urgently needed

Thanx
0
Comment
Question by:Pol_88
  • 4
  • 4
  • 2
  • +2
15 Comments
 
LVL 1

Expert Comment

by:hal3000
ID: 10934509
Hi Pol_88,

A string is an array of chars. The first element of the array is the first char in the string.

If you are having trouble with the "+", I wonder if you are having trouble with the "3" and the "6" strings being passed.

Good luck
0
 
LVL 11

Expert Comment

by:avizit
ID: 10934512
use the c_str() function

but beware it has some pitfalls

so when you use it ..store it in another variable using strdup()

additionally read up  http://www.bcbdev.com/articles/suggest.htm#cstr

/abhijit/

0
 
LVL 11

Expert Comment

by:avizit
ID: 10934516
oops wrong answer .. please ignore
0
 
LVL 11

Expert Comment

by:avizit
ID: 10934564
Maybe this is what you wanted

#include <stdio.h>

int main(int argc, char **argv){
        int a = atoi(argv[1]);
        int b = atoi(argv[3]);

        printf("a is: %i\n",a);
        printf("b is: %i\n",b);
        printf("c is: %c\n",argv[2][0]);
        return 0;
}

if i run it like a.out 3 + 6  i get the following output

$ ./a.exe  3 + 6
a is: 3
b is: 6
c is: +

so if you just want the first character of the second srtring just use argv[2][0]

/abhijit/
0
 

Author Comment

by:Pol_88
ID: 10934568
Im not having problems with the 3 and the 6

i brought them in with int main (argc, char* argv[])
only the +

i tried this so far..

int main (argc, char* argv[])
double one, three;
char operator[1];

one = atof(argv[1]);
three = atof(argv[3]);
operator = argv[2];
0
 

Author Comment

by:Pol_88
ID: 10934585
Why wont it let me do  operator = argv[2];  ??
0
Are end users causing IT problems again?

You’ve taken the time to design and update all your end user’s email signatures, only to find out they’re messing up the HTML, changing the font and ruining the imagery. What can you do to prevent this? Find out how you can save your signatures from end users today.

 
LVL 11

Expert Comment

by:avizit
ID: 10934632
you can do

operator = argv[2][0]  to get the first character of the character array argv[2]

/abhijit/
0
 

Author Comment

by:Pol_88
ID: 10934644
Is there anyway of doing this without multidimensional arrays?
0
 

Author Comment

by:Pol_88
ID: 10934953
thanx for trying to help...

i figured it out
....

int main (int argc, char* argv[])
{
char operator;
double one, three, tot;

one = atof(argv[1]);
three=atof(argv[3]);
operator=*argv[2]);

if (operator=='+')
....................
0
 
LVL 10

Accepted Solution

by:
Mercantilum earned 63 total points
ID: 10934964
Your aguments come as an array of pointers to strings. A string being an array of chars...
argv[0] -> "3"
argv[1] -> "+"
argv[2] -> "6"
"3" being an array of 2 chars: { '3', 0 }

So, using arguments you have not much choice.
(by the way, beware that with some operators like '*' you may have some troubles as
  prog.out  3 * 6
will expand * with the list of files in your directory :)

What I would do here is forget the arguments passing and ask user to input it, e.g.

#include <stdio.h>
main()
{
   double a,b;
   char op;

   printf ("Please enter x OP y : ");
   scanf ("%lf %c %lf", &a, &op, &b);
   // check what we got
   printf ("This is what I got: %lf %c %lf\n", a, op, b);
}
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10937773
Using arguments,you'd also have problems if the user types 3*6 without spaces,then all of it will be stored in argv[1].

Use mercantilum's suggestion with scanf,it gets pretty easy that way.
0
 
LVL 9

Assisted Solution

by:ankuratvb
ankuratvb earned 62 total points
ID: 10938649
Using Mercantilum's code,you get the operands directly as double which you can operate on(no atoi required).and you have the operator in a single char.
You could do a switch on the operator and evaluate accordingly:

switch(op)
{
 case '+':res=a+b;break;
 case '/':res=a/b;break;
 default:printf("Invalid Operator");break;
}
0

Featured Post

Do email signature updates give you a headache?

Constantly trying to correctly format email signatures? Spending all of your time at every user’s desk to make updates? Want high-quality HTML signatures on all devices, including on mobiles and Macs? Then, let Exclaimer solve all your email signature problems today!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Dll Dynamic way c/c++ 9 139
In desperate need of help 8 132
How to get time_t value that is a week old 6 164
Best UNIX-compatible free C compiler for Windows or Mac 6 239
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now