Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Puzzled!???!??

Status
Not open for further replies.

HeavenCore

Programmer
Nov 18, 2004
7
GB
a simple program that eliminates charachters from a string retaining numeric vlues only to avoid error in a conversion to int. I made this program but it wont work, and i have no idea why, any ideas? any help would be great!?

#include <stdlib.h>
#include <stdio.h>
#include <iostream.h>

int calc(int, int, char);


int main(void)
{char *string = "23"
;
double a, b, answer, quit = 1;
double readyvalue;
char c;

while (quit != 0) {
cout <<"calc"<<endl;
cout <<"enter two numbers"<<endl;
cout <<"First Number: "<<endl;
cin >> string;
readyvalue = atoi(string);
a = readyvalue;
cout<<"Enter opperator: +, -, /, * "<<endl;
cin >> c;
cout <<"Second Number: "<<endl;
cin >> string;
readyvalue = atoi(string);
b = readyvalue;
answer = calc(a, b, c);
cout <<"Your answer is :"<<answer <<endl << "would you like to do another? (0 to quit, 1 to continue)"<<endl;
cin >> quit;
}
return 0;
}

int calc(int a, int b, char c)
{
int answer;

switch (c)
{
case '+' : answer = a + b; break;
case '-' : answer = a - b; break;
case '/' : answer = a / b; break;
case '*' : answer = a * b; break;
default: cout <<"no operator!!" << endl; answer = 0; break;
}
return answer;
}
 
> char *string = "23"
This is no use for reading data in from the outside world.
Depending on your OS, you may find a segmentation fault (or something like it) when you try and modify the string.

Try
Code:
char string[80];

PS.
Don't forget the [tt][ignore]
Code:
[/ignore][/tt]
tags when posting code.

--
 
cheers, changed it to a defined array size work perfect, i am using winxp pro, curious why my pointer array would not work but the following msdn sample code does:
Code:
#include <stdlib.h>
#include <stdio.h>
#include <iostream.h>

int main( void )
{
   char *s; double x; int i, g; long l;

   s = "  -2309.12E-15";    /* Test of atof */
   x = atof( s );
   printf( "atof test: \"%s\"; float:  %e\n", s, x );

   s = "7.8912654773d210";  /* Test of atof */
   x = atof( s );
   printf( "atof test: \"%s\"; float:  %e\n", s, x );

   s = "  -9885 pigs";      /* Test of atoi */
   i = atoi( s );
   printf( "atoi test: \"%s\"; integer: %d\n", s, i );

   s = "98854 dollars";     /* Test of atol */
   l = atol( s );
   printf( "atol test: \"%s\"; long: %ld\n", s, l );
   cin >> g;
}
anyways, simple fix for an annoying problem, got to get it working on .net now lol
 
Code:
const char* str = "A string literal like this";
is defined by the langauge to be constant. You're not supposed to be able to change it. The compiler can put it into a read-only part of memory, which would get you the segmentation fault Salem mentioned.

That's why it's pretty incorrect to have a [tt]char*[/tt] pointing to a string literal; it should be a [tt]const char*[/tt] instead, like in my code example above.


The code in your second post works because you aren't changing the strings themselves, you're just changing which string the pointer is pointing to.

Even so, they should still be using [tt]const char*[/tt]. Sadly, many (most?) compilers accept plain [tt]char*[/tt] for backward compatibility with C, which mistakenly allowed that.


Finally, you shouldn't be using <iostream.h> anymore; it's deprecated. Use <iostream> (without the .h) instead.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top