File handling storage issue in c++

Vicky One

Vicky One

@vicky-one-9xiLwc • Oct 26, 2024

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<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<fstream.h>

int hun();
int thou();
char f;


int main()
{
    char in,x;
{
 

    cout<<endl<<"Please enter the alphabet"<<endl;
    cin>>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<<f;
  out.close();

}
}
int hun()
{ int seed;
seed=4;
srand(seed);
for(int i=1; i<2; i++)
{
  int res1=rand()%((100+1)-(1))+1;
  f=res1;
  cout<<"                            "<<res1;



  if(i%2==0)

{
  break;
}
}
return f;
}
int thou()
{ int seed;
seed=10;
srand(seed);

for(int i=1; i<2; i++)

{
  int res2=rand()%((2000+1)-(1000))+1000;
f=res2;
  cout<<"                            "<<res2;
   if(i%2==0)
{
  break;
}
}

  return f;
}

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Anand Tamariya

    Anand Tamariya

    @anand-tamariya-DnfjEX Oct 2, 2013

    else
    {
    exit(1);
    }

    Is this really necessary? You're closing the filestream before writing anything to it.

  • Vicky One

    Vicky One

    @vicky-one-9xiLwc Oct 2, 2013

    Well there is no issue bcz of exit(1); removing it also doesnot store the value.. Can u make it to work??

  • rahul69

    rahul69

    @rahul69-97fAOs Oct 2, 2013

    Vicky OneWell there is no issue bcz of exit(1); removing it also doesnot store the value.. Can u make it to work??

    First of all, whenever u share your code, write it inside [ code ] tags, as it is better to read.
    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

    Anand Tamariya

    @anand-tamariya-DnfjEX Oct 3, 2013

    Vicky OneWell there is no issue bcz of exit(1); removing it also doesnot store the value.. Can u make it to work??

    You need to learn about variable scope and precedence. You are modifying local variable but writing global variable to file.

  • Vicky One

    Vicky One

    @vicky-one-9xiLwc Oct 4, 2013

    Well 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

    Vicky One

    @vicky-one-9xiLwc Oct 4, 2013

    [ 
     
    #include<iostream.h>
     
    #include<conio.h>
     
    #include<fstream.h>
     
    #include<stdlib.h>
     
    int vowel();
     
    int constt();
     
    int main()
     
    {
     
    char x;
     
    cout<<"Please enter an alphabet"<<endl;
     
    cin>>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<<word<<" "<<res1;
     
    cout<<res1;
     
    }
     
    int constt()
     
    {
     
    char word[500];
     
    srand(time(0));
     
    int res2=rand()%((2000+1)-(1000))+1000;
     
    cout<<res2;
     
     
    ifstream in;
     
    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<<word<<" "<<res2;
     
     
    }
     
    ]