help!!!

my infile isn't working

Page 1 of 1

1 Replies - 1015 Views - Last Post: 17 May 2001 - 02:18 PM Rate Topic: -----

#1 miaka284  Icon User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 530
  • Joined: 10-May 01

help!!!

Posted 17 May 2001 - 01:04 PM

okay, my problems is that my infile refused to read my numbers. I had 8,12,16 typed in, in my file. It only read the first number and the rest it gives me zeros and I don't know why.
Please help me!!! Here is my code.....
void factorial()//asks the user to enter a positive number
{
long int input;//declares input local
infile.open("Factofile.txt", ios::in);
cout<<"Please enter a positive number.
";
infile>>input;

if(input==0)//if statement, for if input is 0 {//it doesn't calculate the number 0 cout<<"By definition, the factorial of 0 is 1. ";

} if(input>0)//if input is bigger than zero then it goes to the next function { fact(input); }

if(input<0)//if input is smaller than zero then it kicks out { cout<<"You can't calculate factorials with a negative number. "; factorial(); } } long int fact(long int input1)//here is the function that calculates factorial { for(long int a = input1-1; a>1; a--)//for loop for doing the number again {//until the number is equal to one input1 *= a; } cout<<"The answer is"<<setw(5)<<input1<<endl; info();//calls the function that infile.close(); //closes the infile//asks the user if they want to play again

return input1;

}



Is This A Good Question/Topic? 0
  • +

Replies To: help!!!

#2 Null and Void  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 204
  • Joined: 29-April 01

Re: help!!!

Posted 17 May 2001 - 02:18 PM

Assuming that your code is correct in most ways (I didn't read it) you can get around this by using spaces or endlines in the file instead of commas.

Or, this is how I'd open, and read two numbers from a file, each being seperated by a comma (With C ANSI Standard I/O). This probably isn't applicable to your code, but it may give you a hint or something:
[code]
#include <stdio.h>

// ...

int a, b;
FILE *fp = fopen("somefile.txt","rt"); // Open the file
if(fp!=NULL) { // If we opened it
 // Read an integer, ignore a character (the comma), and read another integer
 fscanf(fp,"%d%*c%d",&a,&b);
 fclose(fp); // Close it
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1