Simple File Stream

Hi, i am writing a program that will ask the user for their shoe size and brand they would like to purchase and store it in a file. it was working but all of a sudden it stopped functioning properly. anything wrong you guys see? It's not writing to the file correctly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

void SHOE_writeFile()
{
		char * fileName = "kicksInfo.txt";
		ofstream getFile(fileName, ios_base::ate|ios_base::trunc);
		char shoeName[25];
		cout <<"Which shoe are you trying to purchase: ";
		cin.getline(shoeName, 25);
		getFile <<endl;
		getFile <<"Shoe: " <<shoeName;

		if (getFile.bad())
		{
			cerr <<"Unable to write to " <<fileName;
			getFile.close();
                        cin.clear();
		}
			
	
}
void SIZE_writeFile()
{
		char * fileName = "kicksInfo.txt";
		float size = 0;
		int valid = 0;
		

		ofstream getFile(fileName, ios_base::ate|ios_base::trunc);
		
	while (valid == 0)
		{
			cout <<"Size: ";
			cin >>size;

			if(!cin)
			{
				cin.clear();
				string junk;
				getline(cin, junk);
			}
			
		if (size == 6 || size == 6.5 || size == 7 || size == 7.5 || size == 8 ||size == 8.5 || size == 9 || size == 9.5 || size == 10 || size == 10.5 || size == 11 || size == 11.5 || size == 12 || size == 12.5|| size == 13)
		{
			valid = 1;
		}

		if (valid != 1)
		{
			cerr<<size <<" isn't a valid shoe size." <<endl;
		}

		if (valid == 1)
		{
			getFile <<"Size: " <<size;
			getFile.flush();
			cin.clear();
		}
		if (getFile.bad())
		{
			cerr<<"Unable to write to " <<fileName;
			getFile.close();
		
		}

		getFile.close ();

		}
}
int main ()
{
	SIZE_writeFile ();
	SHOE_writeFile ();
}
I don't understand why you made such a big code. Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<iostream>
#include<fstream>

using namespace std;

int main()
{
  ofstream size_out("Shoe.out" , ios::app); //ios::app is for appending the file(write at the end of the file)
  ofstream brand_out ("Shoe.out" , ios::app);

  string size_shoe;
  string brand;

  cout<<"Enter the size:"<<endl;
  cin>>size_shoe;
  cout<<"Enter the brand:"<<endl;
  cin>>brand;

  size_out<<"size:"<<size_shoe<<endl;
  brand_out<<"brand:"<<brand<<endl;

  size_out.close();
  brand_out.close();
  return 0;
}
But if you want to make it with your code i will tell you what is the mistake(maybe not the only one).
Firstly you use only if , but you should know that when there's already an if statement you must use else if
Secondly
 stdafx.h 
doesn't exist.
Last edited on
mine is longer because i wanted the input to be valid (no letters or symbols for shoe size)
@danielc99: Ok but did you read what i believe is wrong in your code?
Last edited on
Topic archived. No new replies allowed.