File handling storage issue in c++
i want my random number genereted on the selection bases of vowel or consonent, should be stored in a file permanently on hard disk the below code creates a file but does not store any thing, what should i do? i tried in different ways like without using global variable "f" or declaring the file opening code in both the functions independently insted of a single code in main but all that did not work, file is created but nothing stored.
#include#include #include #include #include int hun(); int thou(); char f; int main() { char in,x; { cout< >in; x=in; if (x=='a'||x=='e'||x=='i'||x=='o'||x=='u') { hun(); } else { thou(); } ofstream out; out.open("D:\\alphabet.txt"); if(!out) { cout<<"Error opening file"; } else { exit(1); } out<
Replies
-
Anand Tamariya
else { exit(1); }
Is this really necessary? You're closing the filestream before writing anything to it. -
Vicky OneWell there is no issue bcz of exit(1); removing it also doesnot store the value.. Can u make it to work??
-
rahul69
First of all, whenever u share your code, write it inside [ code ] tags, as it is better to read.Vicky OneWell there is no issue bcz of exit(1); removing it also doesnot store the value.. Can u make it to work??
Secondly u should indent your code, so that it gets easy to understand.
Now as u said that u wanted "random number generated", then why have u declared f as char? Shouldn't it be int ?
Also Anand is right, u have used exit() function at wrong place.
So make these corrections, and see if u get the right output 😀
Good Luck! -
Anand Tamariya
You need to learn about variable scope and precedence. You are modifying local variable but writing global variable to file.Vicky OneWell there is no issue bcz of exit(1); removing it also doesnot store the value.. Can u make it to work?? -
Vicky OneWell thx a lot.. I had done that like this....If any suggestion for this code like code repetition problem or any thing else u think is not right then please comment it here for more perfection.....overall it worked as required.
-
Vicky One
[ #include
#include #include #include int vowel(); int constt(); int main() { char x; cout<<"Please enter an alphabet"< >x; if(x=='a'||x=='e'||x=='i'||x=='o'||x=='u') { vowel(); }else { constt(); } } int vowel() { char word[500]; srand(time(0)); int res1=rand()%100; ifstream in; //Data from Hard to Ram in.open("F:\\abc.txt"); while( !in.eof() ) in.getline(word,500); ofstream out; //Data from Ram to HArd Disk out.open("F:\\abc.txt"); out<
You are reading an archived discussion.
Related Posts
Quote:
Using simple radio receivers, a laptop and relatively inexpensive Internet technologies, the people of the village have leapfrogged into the 21st century by setting up what amounts to their...
I want to have wi-fi connection in my Laptop. At home I am using BSNL broadband modem. Can someone give me step by step guide, how to install Wi-fi network?
A Fora video talk on entrepreneurship:
https://fora.tv/2013/09/26/The_Corne...il&utm_term=0_de23702f3b-16475fd76d-295725241
Chinese smartphone manufacturer Gionee has launched its latest flagship device in the Indian market- the Elife E6, for Rs 22,999/-. The handset sports a uni-body design, and features a 5-inch...
hi,
we are aware that in frequency modulation, frequency of carrier is made proportional to message signals (without adding any constant term). Similar are the principles of phase modulation, angle...