| | |
Command line calculator
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2004
Posts: 45
Reputation:
Solved Threads: 1
I am trying to make a command line calculator, and I made up a partial code for it, just for adding numbers, I am jsut taking it one step at a time. But I am suppose a number then a space then the math function then a space then the second number. But for some reason I am always getting one for the output. Can anybody tell me what I am doing wrong with this.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
main(int argc, char *argv)
{
int answer;
if(argv[2]=='+')
answer=argv[1]+argv[3];
printf("%i",answer);
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
main(int argc, char *argv)
{
int answer;
if(argv[2]=='+')
answer=argv[1]+argv[3];
printf("%i",answer);
}
argv[1] and argv[3], if available, are strings. Convert them to integers before attempting to add them; atoi may be useful for this.
argv[2] is a string also. Use argv[2][0] for the first character.
[edit]D'oh! Didn't even look at main, which should be declared like this:
argv[2] is a string also. Use argv[2][0] for the first character.
[edit]D'oh! Didn't even look at main, which should be declared like this:
int main(int argc, char *argv[])![]() |
Similar Threads
- C command-line I/O question (C++)
- Using WGET command line util problem (Windows NT / 2000 / XP)
- Using command line parameters (C++)
- Command Line Parameter (C++)
- Help! How to make a csh receive argvs from command line? (*nix Software)
- Three New Command-Line Tools (Windows tips 'n' tweaks)
- command line (OS X)
Other Threads in the C Forum
- Previous Thread: concatenating string and others
- Next Thread: .txt file formatting assignment due TODAY help!!
| Thread Tools | Search this Thread |
Tag cloud for C
#include ansi array arrays asterisks binarysearch calculate centimeter changingto char command convert copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic fflush file fork forloop framework functions getlasterror givemetehcodez grade graphics gtkgcurlcompiling hacking hardware histogram homework inches include incrementoperators input iso kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. lowest match matrix microsoft motherboard mysql number opendocumentformat opensource owf pattern pdf performance pointer posix problem probleminc process program programming radix recursion recv research reversing scanf scripting segmentationfault sequential shape socket socketprograming spoonfeeding standard string strings structures student systemcall testing threads turboc unix user variable voidmain() wab windows.h windowsapi






