Msaada kwenye hii code ya c++


elmagnifico

elmagnifico

JF-Expert Member
Joined
Jul 7, 2011
Messages
7,894
Likes
7,629
Points
280
Age
30
elmagnifico

elmagnifico

JF-Expert Member
Joined Jul 7, 2011
7,894 7,629 280
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;
}
 
Mad Max

Mad Max

JF-Expert Member
Joined
Oct 21, 2010
Messages
3,736
Likes
781
Points
280
Mad Max

Mad Max

JF-Expert Member
Joined Oct 21, 2010
3,736 781 280
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?
 
Stefano Mtangoo

Stefano Mtangoo

Verified Member
Joined
Oct 25, 2012
Messages
3,689
Likes
908
Points
280
Stefano Mtangoo

Stefano Mtangoo

Verified Member
Joined Oct 25, 2012
3,689 908 280
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);
 
elmagnifico

elmagnifico

JF-Expert Member
Joined
Jul 7, 2011
Messages
7,894
Likes
7,629
Points
280
Age
30
elmagnifico

elmagnifico

JF-Expert Member
Joined Jul 7, 2011
7,894 7,629 280
Asante sana sa hivi in a run kama nilivyotaka na ina print results vizuri
 
N

Nyasiro

Verified Member
Joined
Feb 20, 2012
Messages
1,287
Likes
32
Points
145
N

Nyasiro

Verified Member
Joined Feb 20, 2012
1,287 32 145
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");
 
Stefano Mtangoo

Stefano Mtangoo

Verified Member
Joined
Oct 25, 2012
Messages
3,689
Likes
908
Points
280
Stefano Mtangoo

Stefano Mtangoo

Verified Member
Joined Oct 25, 2012
3,689 908 280
system should be small letters. how did it even compile?
 
N

Nyasiro

Verified Member
Joined
Feb 20, 2012
Messages
1,287
Likes
32
Points
145
N

Nyasiro

Verified Member
Joined Feb 20, 2012
1,287 32 145
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..!!
 
Stefano Mtangoo

Stefano Mtangoo

Verified Member
Joined
Oct 25, 2012
Messages
3,689
Likes
908
Points
280
Stefano Mtangoo

Stefano Mtangoo

Verified Member
Joined Oct 25, 2012
3,689 908 280
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!
 
N

Nyasiro

Verified Member
Joined
Feb 20, 2012
Messages
1,287
Likes
32
Points
145
N

Nyasiro

Verified Member
Joined Feb 20, 2012
1,287 32 145
Ok. Ntajaribu tena.
 

Forum statistics

Threads 1,251,605
Members 481,811
Posts 29,777,746