954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

fstream to int

Hello I have created a sucsessful prime number finder however I wish to retrive the start number from a file the last line shows how I tried to do this however it failed any solutions ?

/* By Kelechi Nze 
	My first prime project*/
#include <windows.h>
#include <iostream>
#include <fstream>
#include <string>
#define NEWLINE "\n"
using namespace std;
struct primes //My Prime number structure
	{
		fstream startnumb; //The starting number file
		ofstream passnumb; //Passnumb: where primes are stored
		int number, factor, divider;	/*Number: The actulal prime number "finder" 
											factor: What's a factor
											Divider: What to divide the prime.number with to find if there is a prime there*/
	}prime;	//Only one member in the structure ? Yes !
	prime.passnumb.open("UserStorage.txt");
	prime.startnumb.open("startnumber.ini")
        prime.number = prime.startnumb;
kelechi96
Light Poster
45 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

Change line 19 to:
prime.startnumb >> prime.number;

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

It's maybe better when you change line 11 to: ifstream startnumb;

You shouldn't forget to close the files when you don't need them anymore ...

BTW, on line 7 you're using #define to declare a constant, it's better to use const instead of #define ...

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

It's maybe better when you change line 11 to: ifstream startnumb;

You shouldn't forget to close the files when you don't need them anymore ...


Thank You !
I'm going to change the number using fstream later and also thats not all of my code I closed the file later on. Someone close this thread.

kelechi96
Light Poster
45 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

It's maybe better when you change line 11 to: ifstream startnumb;

You shouldn't forget to close the files when you don't need them anymore ...

BTW, on line 7 you're using #define to declare a constant, it's better to use const instead of #define ...

Why ?

kelechi96
Light Poster
45 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 
Why ?

Consider the following program using #define :

#include <iostream>

using namespace std;

#define BEGIN 20
#define END 60
#define LENGTH END-BEGIN

int main()
{
	cout << "LENGTH = " << LENGTH << endl;
	cout << "2*LENGTH = " << 2*LENGTH << endl;
	return 0;
}

Well, it doesn't return the result you expected ...
You probably expected it would return 80 ...

You don't have this problem using const :

#include <iostream>

using namespace std;

int main()
{
	const int BEGIN = 20;
	const int END = 60;
	const int LENGTH = END-BEGIN;
	
	cout << "LENGTH = " << LENGTH << endl;
	cout << "2*LENGTH = " << 2*LENGTH << endl;
	return 0;
}


Hope this helps !

BTW, const is invented to avoid the use of #define , so why would you continue using #define if you can use a better (and more C++ like instruction) ??

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

Thanks again +rep

kelechi96
Light Poster
45 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You