Saving a string to file?

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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
void ascii (int number);
bool raffle (int number);
const int cArray=5;




int main ()	
{		
	
	int value;
	
	char option;
	
while (1)
{

	
	cout <<"Enter a positive integer number: " <<endl;
	cin >>value;
	cout <<endl;
	
	
	cout <<"A[scii]" "\t\tR[affle]" "\t\tE[xit]" <<endl;
	cout <<"Please select an option: " <<endl;
	cin >>option;
	cout<<endl;

	switch (option)
	{
		case 'a':
		case 'A':

		    ascii(value);
			break;

		case 'r':
		case 'R':
			ofstream outfile("G:/File3.txt", ios::out);
			if(!outfile)
			{
				cout<<"File could not be opened"<<endl;
				exit(1);
			}
			if (raffle(value)==1)
			{
				outfile<<"The number "<<value<<"is present in the array."<<endl;
			}
			else
			{
				outfile<<"The number "<<value<<"is not present in the array."<<endl;
			}
			outfile.close();
			break;
			
		case 'e':
		case 'E':
			
			return 0;
			break;


	}
}
}


void ascii (int value)
{
	 
	if (48 <= value && value <= 57)
	{
		cout <<"The number you have entered corresponds to a digit in the ASCII table." <<endl;
	}
	else if(65 <= value && value <= 90)
	{
		cout <<"The number you have entered corresponds to an uppercase letter in the ASCII table." <<endl;
	}
	else if (97 <= value && value <= 122)
	{
		cout <<"The number you have entered corresponds to a lowercase letter in the ASCII table." <<endl;
	}
	else  
	{
		cout <<"The number you have entered corresponds to none of the above." << endl;
	}
	
}

bool raffle (int value)
{	
	int random[cArray];
	srand(time(NULL));
	for (int i=0; i<5; i++)
	{
		random[i]= 0+rand()%(100+1-0);
		cout<<random[i]<<" "<<endl;
	}
	
	for (int j=0; j<5; j++)
	{
		if (value == random[j])
		{
			cout << "\n" <<j<<endl;
			
			return true;
		}
	}
			cout << "Number not present."<<endl;
			return false;
			
			
		
}

My problem is that under the switch option R, I need the program to save a sentence to a file. Everything else runs good, except it won't save the proper sentence to file3. Any suggestions would be greatly appreciated, and sorry for the long code! Thank you!
I took this program and ran it with Code::Blocks and a MinGW compiler, and it was spitting out some garbage about the switch logic and fstream being incompatible. Replace your switch with if () and elseif() and that should fix this problem. Also change the "raffle(value) == 1" to "raffle(value) == true" for readability.
Wolfgang,
Thank you so much! I made the changes you suggested and it finally worked! Thank you so much once again!! :D
Topic archived. No new replies allowed.