-
simple char question
How would you define a string so that it is just the size of the user input??
right now in my calc program i have
Code:
int main()
{
char calc[50];
cout<<"Calc: ";
cin>>calc;
for(int i=0;i<50;i++)
{
cout<<calc[i];
}
getchar();
return 0;
}
but ofcourse as soon as there are empty spaces it starts displaying weird characters which i don't want it to.
I just started this but its supposed to become a calculator which accepts inputs such as: 3*3+8
Original Idea coming from: joshdick
-Devouring One-
-
You should just zero out the contents when you declare it. The best way to do it is as follows:
Code:
char buffer[256] = { 0 };
That will set every element to 0 and you won't get the garbage stuff.
-
-
Is there anyway to check if a character is a certain type??
In my case I would want it to check if it were *,/,-,+ any of these.
I know there is ctype.h but it hasn't helped me much
-
Yeah its not a big deal. * / + - , those are all just characters right? So you can do an easy check.
Code:
if( calc[ i ] == '*' )
{
// Do something creative
}
Assuming i is any valid range in your array. Don't attempt to compare strings like this but it will work for single characters.
-
Arg... something is wrong but i can't figure out what
Code:
#include <iostream.h>
#include <conio.h>
int main()
{
char num[20]= { 0 };
double temp = 0;
cout<<"Calc: ";
cin>>num;
for(int i=0;i<50;i++)
{
if(num[i] == '*')
{
temp = (num[i-1])*(num[i+1]);
}
if(num[i] == '/')
{
temp = (num[i-1])/(num[i+1]);
}
if(num[i] == '+')
{
temp = (num[i-1])+(num[i+1]);
}
if(num[i] == '-')
{
temp = (num[i-1])-(num[i+1]);
}
}
cout<<"\n"<<temp;
getchar();
return 0;
}
-
Be very careful with arrays!!
Look at your loop with i = 0.
Code:
if(num[i] == '*')
{
temp = (num[i-1])*(num[i+1]);
}
num[i-1] thats not in the bounds of the array!! You need to make sure you don't do this sort of thing.
-
Ok even if i make as simple as only being able to do stuff like 3+3
it gives me answers like 94 and higher
Code:
#include <iostream.h>
#include <conio.h>
int main()
{
char num[20]= { 0 };
double temp = 0;
cout<<"Calc: ";
cin>>num;
for(int i=0;i<50;i++)
{
if(num[i] == '*')
{
temp = (num[0])*(num[2]);
}
if(num[i] == '/')
{
temp = (num[0])/(num[2]);
}
if(num[i] == '+')
{
temp = (num[0])+(num[2]);
}
if(num[i] == '-')
{
temp = (num[0])-(num[2]);
}
}
cout<<"\n"<<temp;
getchar();
return 0;
}
-
i woked on it some more and now i have something like this which makes sence but it doesn't work:
Code:
if(num[i] == '-')
{
x = i-1;
y = i+1;
temp = (num[x])-(num[y]);
}
-
Okay a few things then I'm going to sleep. PM me and I can get back to you tomorrow if you have more questions.
Now look at your for loop, you still go to 50 but you only have 20 elements! Change that down to 20. Also, you are adding the ascii values of the characters '3' and '3' right now. The ascii value of '3' is 51. This is obviously not what we want here. We need to convert it to floating point. We can use atof found in the stdlib.h to do this. So our if statement will look like this:
Code:
if(num[i] == '+')
{
temp = atof(&num[0]) + atof(&num[2]);
}
Note I am using &num[0], need to pass the address of it into the function because it requires a pointer to a character string. If you want to see more about the function prototype or anything you can look on msdn.
msdn.microsoft.com
Good Luck.