CrazyEngineers
  • 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
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Anand Tamariya

    MemberOct 2, 2013

    else
    {
    exit(1);
    }
    Is this really necessary? You're closing the filestream before writing anything to it.
    Are you sure? This action cannot be undone.
    Cancel
  • Vicky One

    MemberOct 2, 2013

    Well there is no issue bcz of exit(1); removing it also doesnot store the value.. Can u make it to work??
    Are you sure? This action cannot be undone.
    Cancel
  • rahul69

    MemberOct 2, 2013

    Vicky One
    Well 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!
    Are you sure? This action cannot be undone.
    Cancel
  • Anand Tamariya

    MemberOct 3, 2013

    Vicky One
    Well 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.
    Are you sure? This action cannot be undone.
    Cancel
  • Vicky One

    MemberOct 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.
    Are you sure? This action cannot be undone.
    Cancel
  • Vicky One

    MemberOct 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;
     
     
    }
     
    ]
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register