Home › Forums › C Programming › Some problems in C++
- This topic has 14 replies, 2 voices, and was last updated 15 years, 6 months ago by 050449.
- AuthorPosts
- January 19, 2009 at 6:35 pm #2175050449Participant
I started C++ two days ago and need your help
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556<br />#include <iostream><br />#include <fstream><br />#include <string.h><br />#include <string><br />#include <stdio.h><br />#include <stdlib.h><br /><br /><br />using namespace std;<br />char sendtext;<br />char username;<br />void read(){<br />system("cls");<br />cout << "You autorised as:" << username << "n";<br />ifstream in;<br />in.open("msngr.txt");<br />if (!in) {<br />cout << "Input file cannot be opened.n";<br />//return(1);<br />}<br />char str[20000];<br />while (!in.eof()) {<br />in >> str;<br />cout << str << "n";<br />strcpy(str, "");<br />}<br />cout << "n";<br />in.close();<br /><br />}<br />void<br />write(){<br /><br />cin >> sendtext;<br />if(sendtext){<br />FILE * pFile;<br />char yazi[] = {sendtext , 'n'};<br />pFile = fopen ( "msngr.txt" , "a+" );<br />fwrite (yazi , 1 , sizeof(yazi) , pFile );<br />fclose (pFile);<br />read();<br />write();<br />}<br />}<br />main(){<br />cout << "Enter your username: ";<br />cin >> username;<br />read();<br />write();<br /><br /><br />return (0);<br />}<br /><br /></string></fstream></iostream>why its not work properly? Please help.
- January 21, 2009 at 9:35 pm #3506GWILouisaxwzklaParticipant
The first function “read” looks ok to output the file data one word per line ,except I would probably pass the user’s name as a parameter instead of a global variable :
123<br />void read( char * userName );<br />the second function for adding data to a file ( not sure why you used both fstream.h and stdio.h for file input / output in the same program ( ? ) ) seems to have some problems. First I would allocate an array of the proper size and use strcpy ( string.h ) to copy the data.
1234<br />char yazi [ MAX_SIZE ];<br />strcpy ( yazi , sendText );<br />Next , you should take read() and write() out of the conditional or the program will never end :
12345678910111213<br />if ( sendtext )<br />{<br />FILE * pFile;<br />char yazi[] = {sendtext , 'n'};<br />pFile = fopen ( "msngr.txt" , "a+" );<br />fwrite (yazi , 1 , sizeof(yazi) , pFile );<br />fclose (pFile);<br />read(); //take these out!<br />write();<br />}<br /><br />thats what I see so far…..
- January 23, 2009 at 8:50 pm #3507050449Participant
thanks for all but there is error
`MAX_SIZE’ undeclared (first use this function)
- January 23, 2009 at 8:50 pm #3508050449Participant
thanks for all but there is error
`MAX_SIZE’ undeclared (first use this function)
- January 23, 2009 at 8:54 pm #3509050449Participant
i defined MAX_SIZE and this problem solved but now it shows me
invalid conversion from
char’ to
const char*’
initializing argument 2 of `char* strcpy(char*, const char*)’ - January 23, 2009 at 8:59 pm #3510050449Participant
if ( sendtext )
{
FILE * pFile;
char yazi[] = {sendtext , ‘n’};
pFile = fopen ( “msngr.txt” , “a+” );
fwrite (yazi , 1 , sizeof(yazi) , pFile );
fclose (pFile);
read(); //take these out!
write();
}if i take this out my new text will not appear
- January 24, 2009 at 9:11 pm #3511GWILouisaxwzklaParticipant
for the write function I would do something like ( I don’t think I would have the console prompt inside the function , but if this is what you want ):
123456789101112131415161718192021222324252627282930313233<br /><br />void write ()<br />{<br /><br />char choice;<br />cout << "do you want to input data to file ( y or n )" << endl;<br />cin >> choice;<br />cin.get(); //get newline<br />while ( choice != 'y' && choice != 'n' )<br />{<br />cout << "bad choice!" << endl;<br />cout << "do you want to input data to file ( y or n )" << endl;<br />cin >> choice;<br />cin.get(); //get newline<br />}<br /><br />if ( choice == 'y' )<br />{<br />char inputText [ MAX_NAME ];<br />cout << "enter input text: " << endl;<br />cin.getline ( inputText , MAX_NAME , 'n' );<br /><br />ofstream outputFile;<br />//PUT YOUR DATA FILE NAME HERE!<br />outputFile.open ( "c:\programs\help\data3.txt" , ios::app );<br />outputFile << inputText << endl;<br />outputFile.close();<br /><br />}<br /><br />}<br /> - January 25, 2009 at 7:21 am #3512050449Participant
thanks it works ok.
now code is12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273<br />#include <iostream><br />#include <fstream><br />#include <string.h><br />#include <stdlib.h><br />#define MAX_NAME 1000<br /><br /><br />using namespace std;<br />char sendtext;<br />char username;<br /><br />void read(){<br />system("cls");<br />cout << "You autorised as:" << username << "n";<br />ifstream in;<br />in.open("msngr.txt");<br />if (!in) {<br />cout << "Input file cannot be opened.n";<br />//return(1);<br />}<br />char str[20000];<br />while (!in.eof()) {<br />in >> str;<br />cout << str << "n";<br />strcpy(str, "");<br />}<br />cout << "n";<br />in.close();<br /><br />}<br />void write ()<br />{<br /><br />char choice;<br />cout << "do you want to input data to file ( y or n )" << endl;<br />cin >> choice;<br />cin.get(); //get newline<br />while ( choice != 'y' && choice != 'n' )<br />{<br />cout << "bad choice!" << endl;<br />cout << "do you want to input data to file ( y or n )" << endl;<br />cin >> choice;<br />cin.get(); //get newline<br />}<br /><br />if ( choice == 'y' )<br />{<br />char inputText [ MAX_NAME ];<br />cout << "enter input text: " << endl;<br />cin.getline ( inputText , MAX_NAME , 'n' );<br /><br />ofstream outputFile;<br />//PUT YOUR DATA FILE NAME HERE!<br />outputFile.open ( "msngr.txt" , ios::app );<br />outputFile << inputText << endl;<br />outputFile.close();<br />read();<br />write();<br />}<br /><br />}<br />main(){<br />cout << "Enter your username: ";<br />cin >> username;<br />read();<br />write();<br /><br /><br />return (0);<br />}<br /><br /></fstream></iostream>but there is some problems too.
when i enter username it writes only first the letter of the username and all other letters understands as answer to the confirmation and writesYou autorised as:N
do you want to input data to file ( y or n )
bad choice!
do you want to input data to file ( y or n )
bad choice!
do you want to input data to file ( y or n )and when it shows the data of the file it puts n after all words and when i write “something is wrong”
it shows
something
is
wrong
but in file everything is ok - January 27, 2009 at 8:48 pm #3513GWILouisaxwzklaParticipant
This seems to work:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980<br /><br />#include <iostream><br />#include <fstream><br />#include <string.h><br />#include <stdlib.h><br />#include <ctype.h><br />#define MAX_NAME 1000<br /><br /><br />using namespace std;<br />char sendtext;<br />char username [ MAX_NAME ];<br /><br />void read(){<br />system("cls");<br />cout << "You autorised as:" << username << "n";<br />ifstream in;<br />in.open("c:\programs\in1.txt");<br />if (!in) {<br />cout << "Input file cannot be opened.n";<br />//return 1;<br />}<br />char str[20000];<br />while (!in.eof()) {<br />in >> str;<br />cout << str << "n";<br /><br />}<br />cout << "n";<br />in.close();<br /><br />}<br />void write ()<br />{<br />char badInput;<br />char choice;<br />cout << "do you want to input data to file ( y or n )" << endl;<br />cin >> choice;<br />cin.get(); //get newline<br />while ( tolower ( choice ) != 'y' && tolower ( choice ) != 'n' )<br />{<br />badInput = cin.get();<br />while ( badInput != 'n' ) //clear garbage out of stream<br />{<br />badInput = cin.get();<br />}<br />cout << "bad choice!" << endl;<br />cout << "do you want to input data to file ( y or n )" << endl;<br />cin >> choice;<br />cin.get(); //get newline<br />}<br /><br />if ( tolower ( choice ) == 'y' )<br />{<br />char inputText [ MAX_NAME ];<br />cout << "enter input text: " << endl;<br />cin.getline ( inputText , MAX_NAME , 'n' );<br /><br />ofstream outputFile;<br />//PUT YOUR DATA FILE NAME HERE!<br />outputFile.open ( "msngr.txt" , ios::app );<br />outputFile << inputText << endl;<br />outputFile.close();<br />read();<br />write();<br />}<br /><br />}<br />main(){<br />cout << "Enter your username: ";<br />cin >> username;<br />cin.get();<br />read();<br />write();<br /><br /><br />return (0);<br />}<br /></fstream></iostream> - January 28, 2009 at 9:08 am #3514050449Participant
Oh thanks it works well.
but can you explain what “cin.get()” means??
and what it used for?and now i have to think how to remove confirmation
- January 28, 2009 at 9:17 am #3515050449Participant
ups problem. problem with read not solved it shows each word in new line
- January 29, 2009 at 9:16 pm #3516GWILouisaxwzklaParticipant
cin.get() is a member function that gets the next character from the input stream:
1234567<br />char ch;<br /><br />ch = cin.get(); //get a single character from the keyboard and store in 'ch'<br />ch = cin.get(); //get a single character from the keyboard , but don't store it ( throw it out )<br /><br /> - January 30, 2009 at 5:14 am #3517050449Participant
thanks.and what about read() function ??
- February 2, 2009 at 7:55 pm #3518GWILouisaxwzklaParticipant
I think read() is for arrays. Check out this link on the topic: http://www.cplusplus.com/reference/iostream/istream/read.html
- May 14, 2009 at 11:27 am #3519050449Participant
Hi. I have a new problem now.
I am using C++ builder 2009 and i cannot make “tmaskedit” to show characters as “*” like in web
Can anyone help me?
- AuthorPosts
- The forum ‘C Programming’ is closed to new topics and replies.