Msaada kwenye hii code ya c++

elmagnifico

JF-Expert Member
Jul 7, 2011
8,260
9,704
Najaribu kutengeneza program ambayo itakuwa ina register watu kwa majina mawili, number za simu na kiasi cha pesa kisha inaandika hizo details kwenye document and then later inasoma hiyo document na kuonyesha dtails ilizokuwa imeandika.
Nimefanikiwa kwenye kuandika ila kwenye kusoma program inashindwa. Naomba msaada wenu wadau wapi nilipokosea code zangu ni hizi:
Code:
#include <iostream>
#include <string>
#include <fstream>

int main()
{
    const char* filename = "regis.txt";
    int x =1;
    while(true)    
    {
    
        std::fstream f(filename, std::fstream::out|std::fstream::app);
    
        std::fstream fs;
    
            fs.open(filename);
    
            
            std::cout<<"Enter 1 to continue or  type 0 anytime to  quit\n";
            std::cin>>x;    
            
            if(x!=1)
            {
                break;
            }
            
            
            if(f.is_open())
            {
                std::cout<<"Enter your first name\n";
                std::string str;
                std::cin>>str;
                f.write(str.c_str(), str.length());
                
                f.write(" ", 1);
                
                std::cout<<"Enter your second name\n";
                std::string ste;
                std::cin>>ste;
                f.write(ste.c_str(), ste.length());
                f.write(" ", 1);
                f.write(" ", 1);
                
                std::cout<<"Enter phone number\n";
                std::string stz;
                std::cin>>stz;
                f.write(stz.c_str(), stz.length());
                f.write(" ", 1);
                f.write(" ", 1);
                
                std::cout<<"Enter the mount received\n";
                std::string stg;
                std::cin>>stg;
                f.write(stg.c_str(), stg.length());
                f.write("\n", 1);
                f.write("\n", 1);
                
                
                f.close();
                
                
            
            }
                
        
    }
    std:: string line;
    std::ifstream fs;
    if (fs.is_open())
    {
        while ( fs.good() )
        {
            std::getline (fs,line);
            std::cout << line << std::endl;
        }
        fs.close();
    }
    else 
    std::cout << "Unable to open file";
    
    return 0;
}
 
Najaribu kutengeneza program ambayo itakuwa ina register watu kwa majina mawili, number za simu na kiasi cha pesa kisha inaandika hizo details kwenye document and then later inasoma hiyo document na kuonyesha dtails ilizokuwa imeandika.
Nimefanikiwa kwenye kuandika ila kwenye kusoma program inashindwa. Naomba msaada wenu wadau wapi nilipokosea code zangu ni hizi:
Code:
#include <iostream>
#include <string>
#include <fstream>

int main()
{
    const char* filename = "regis.txt";
    int x =1;
    while(true)    
    {
    
        std::fstream f(filename, std::fstream::out|std::fstream::app);
    
        std::fstream fs;
    
            fs.open(filename);
    
            
            std::cout<<"Enter 1 to continue or  type 0 anytime to  quit\n";
            std::cin>>x;    
            
            if(x!=1)
            {
                break;
            }
            
            
            if(f.is_open())
            {
                std::cout<<"Enter your first name\n";
                std::string str;
                std::cin>>str;
                f.write(str.c_str(), str.length());
                
                f.write(" ", 1);
                
                std::cout<<"Enter your second name\n";
                std::string ste;
                std::cin>>ste;
                f.write(ste.c_str(), ste.length());
                f.write(" ", 1);
                f.write(" ", 1);
                
                std::cout<<"Enter phone number\n";
                std::string stz;
                std::cin>>stz;
                f.write(stz.c_str(), stz.length());
                f.write(" ", 1);
                f.write(" ", 1);
                
                std::cout<<"Enter the mount received\n";
                std::string stg;
                std::cin>>stg;
                f.write(stg.c_str(), stg.length());
                f.write("\n", 1);
                f.write("\n", 1);
                
                
                f.close();
                
                
            
            }
                
        
    }
    std:: string line;
    std::ifstream fs;
    if (fs.is_open())
    {
        while ( fs.good() )
        {
            std::getline (fs,line);
            std::cout << line << std::endl;
        }
        fs.close();
    }
    else 
    std::cout << "Unable to open file";
    
    return 0;
}

ningekua na turbo au compiler yoyote ningekusaidia... ingawa kitambo kidogo. unaweza nisaidia link ya kudownload direct compiler yoyote?
 
Did you notice that after writing to file you are closing it and before you read it you are not opening it?
so add code below before fs. is_open()

Code:
 fs.open(filename, std::fstream::in);
 
cant print results here in console (using visual studio). how can I use the following instead of using the file stream

Code:
System ("notepad.exe regis.txt");
 
system should be small letters. how did it even compile?

I didn't tried it before. But I could not get it to print the results either or after the code you added. So I thought I could use an external program but still it says notepad.exe is not recognized as internal or external command... added some environmental variable but no lucky. or it needs some includes, using..!!
 
I didn't tried it before. But I could not get it to print the results either or after the code you added. So I thought I could use an external program but still it says notepad.exe is not recognized as internal or external command... added some environmental variable but no lucky. or it needs some includes, using..!!
no its supposed to work after adding that line!
 
Back
Top Bottom