#include <stdio.h>
#include <math.h>

int main()
{
	double op1,op2;
	char opp;
	char *res= "2 ^ 1";

	sscanf(res,"%d %c %d",&op1,&opp,&op2);

	printf("%d\n",op1); 
	printf("%c\n",opp); 
	printf("%d\n",op2); 
	printf("====\n");
	printf("%d\n",pow(op1,op2)); /* the output is for sure 2 */
}


but it output :
1073741824 <-- ??? I don't know what here so please any helping will be appreciated thankx

:?:

Recommended Answers

All 9 Replies

For doubles, use specifiers for doubles, not ints.

sscanf(res,"%lf %c %lf",&op1,&opp,&op2);

	printf("%g\n",op1);
	printf("%c\n",opp);
	printf("%g\n",op2);
	printf("====\n");
	printf("%g\n",pow(op1,op2)); /* the output is for sure 2 */

thankx Dave :p

Davedo you have a simple code that can read a file let's say "xx.txt" line by line? :rolleyes:


Thankx Dave ,but my question is the "%g" means what ?

http://www.rt.com/man/printf.3.html

g      The double argument is converted in style  f  or  e
              (or  E for G conversions).  The precision specifies
              the number of significant digits.  If the precision
              is missing, 6 digits are given; if the precision is
              zero, it is treated as 1.  Style e is used  if  the
              exponent  from  its  conversion  is less than -4 or
              greater than or equal to the  precision.   Trailing
              zeros  are  removed from the fractional part of the
              result; a decimal point appears only if it is  fol-
              lowed by at least one digit.
fputs ( line, stdout ); /* write the line */

I want to save the passed lines into an array
for example:
char *res;

Go with C++ and use a vector of strings. Maybe something similar to this.

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

int main()
{
   static const char filename[] = __FILE__;
   ifstream file(filename);
   string line;
   vector<string> text;
   while ( getline(file, line) )
   {
      text.push_back(line);
   }
   for ( vector<string>::size_type i = 0, size = text.size(); i < size; ++i )
   {
      cout << text[i] << endl;
   }
   return 0;
}

Thankx Dave ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.