final exam


last hope i have is this forum


write a program which read a string contains a number followed by an operation there anther number (e.g "25*3"), the program must preform the operation between the two numbers than print the result

reading code

\

char A[6];
for(i=0;(A[i]=getchar())!='\n';i++);
A[i]='\0';

[/code]






print code


1
2
3
4

for(i=0;B[i]!='\0';i++)
cout<<B[i];





the half processing code that convert string to real number

[code]


char A[10];
int i,n

for(i=0;(s[i]=getchar())!='\n';i++);
A[i]='\0';

for(i=0;A[i]!='\0';i++)
n=n*10+A[i]-'0';





Don't use a char array. Use a proper C++ string.

Read the whole thing into the string at once; not a char at a time.

Then go through the string until you find the operation.

You now know the location in the string of the two numbers.

Get them, as substrings.

Turn them into numbers using std::stoi

Then switch on the operation char to do the correct operation.

they don't wanna string

they wanna a char of array


Last edited on
This is for a class in the C programming language, is it?

kind off, but this question will be in the final 100%

can u help ?
Last edited on
Get input characters.
Copy input from start into new char array until you reach the operator.
Copy input from operator to end into another new char array.

At this point, you have two char arrays, one containing the first nubmer and one containing the second.

Can you do that?
i didn't understand
><"

for(i=0;(a[i]=getchar())!='\n';i++);
a[i]='\0';

for(i=0;a[i]!='\0';i++)
{
if(a[i]>='0'&&a[i]<='9')
z[i]=a[i];

else
m[i]=a[i];


than what ?
for(i=0;(a[i]=getchar())!='\n';i++);

I have no idea what's going on here. Is this meant to be fetching the input from the keyboard?

Firstly, you are not being charged by the letter. Here is a rule you should bear in mind forever; give your variables meaningful names. Do not call the input a.

Here is how to fetch the input in a way that's actually simple and easy to understand, instead of that character by character mess.
1
2
char input[100];
cin >> input;   // do not enter an input that will exceed the size of the array 


At this point, input is what the user typed, with a zero value on the end to mark the end of what they typed.

Do NOT call one of your arrays z. Do NOT call one of your arrays m. Do yourself a favour and give them names that make your code easier to understand, rather than harder.

When you've got your two arrays (with zero value on the end to mark the end of your caracters), turn them into int values. This can be done like this:

int value = std::stoi(std:string(array));
This will require a compiler conformant to C++11. Since the year is 2016, I assume you've got a compiler that supports C++11 (from the year 2011).



mmmmmmmm

thanks for your help but that didn't help a lot i still in the stop point off this question

but thanks for your time and advice
Topic archived. No new replies allowed.