Newbie 2 First Grade!

P

PerritoPerron

void main(){

char Mybuffer[32];
I got this input from user

1+2*3
from this line
cin.getline(Mybuffer, 32 '\n');

My question is how can I compute the result???
Need a little help here u know what I mean...

}

Thanks Fellas...
 
A

Alf P. Steinbach

void main(){

'main' must have result type 'int'.

Some compilers erronously accept 'void'.

char Mybuffer[32];

Use 'std::string', what you have is a buffer that will likely
overflow.

I got this input from user

1+2*3
from this line
cin.getline(Mybuffer, 32 '\n');

No you did not. That won't compile.


My question is how can I compute the result???
Need a little help here u know what I mean...

As Greg P. have answered, check out Bjarne Stroustrup's TCPPPL
book for ideas on how to do that in C++.

But at your current level consider using an interpreted language,
e.g., JScript in Windows,


==================================================================
// Tab = indent = 4
// A _very_ primitive calculator.
// Note: WScript is not part of JScript but is provided by the WSH environment.

var expression;
var stdout = WScript.StdOut;
var stdin = WScript.StdIn;

stdout.Write( "? " );
expression = stdin.ReadLine();
stdout.WriteLine( eval( expression ).toString() );
==================================================================


or VBScript (this also in Windows)


==================================================================
' Tab = indent = 4
' A _very_ primitive calculator.
' Note: WScript is not part of VBScript but is provided by the WSH environment.

option explicit

dim expression, stdout, stdin

set stdout = WScript.StdOut
set stdin = WScript.StdIn

stdout.Write "? "
expression = stdin.ReadLine
stdout.WriteLine Replace( Eval( expression ), ",", "." )
==================================================================


or Perl (more system-independent)


==================================================================
# Tab = indent = 4
# A _very_ primitive calculator.

use strict;

my $expression;

print "? ";
$expression = <STDIN>;
print eval $expression;
==================================================================


Hope this helps...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top