Sunday, May 10, 2009

[C++] Why the result of my programme is "烫烫烫烫烫烫烫烫"?

my C++ code:








#include %26lt;windows.h%26gt;


#include %26lt;iostream%26gt;


#include %26lt;fstream%26gt;


using namespace std;





int main()


{


char a[128]="**** the truth!";


cout%26lt;%26lt;a%26lt;%26lt;endl;





ofstream outfile("temp.txt",ios::out);


for(int i=0;i%26lt;=127;i++)


{if(a[i]!=NULL)outfile%26lt;%26lt;a[i];}


outfile.close();





char b[128];


ifstream infile("temp.txt",ios::in);


for(int j=0;j%26lt;=127;j++)


{infile%26gt;%26gt;b[i];}


cout%26lt;%26lt;b%26lt;%26lt;endl;


infile.close();





return 0;


}











result:


**** the truth!


烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫!烫烫...


烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫蘁

[C++] Why the result of my programme is "烫烫烫烫烫烫烫烫"?
Probably because of this code





for(int j=0;j%26lt;=127;j++)


{infile%26gt;%26gt;b[i];}





Your for loop increments j but in the loop you are using b[i].


i should have a value of 127 or rubbish if it is out of scope.


C++: How do you edit a character string in a different function than the one you declared it in?

The following code is C++:








#include "stdafx.h"


#include %26lt;iostream%26gt;


#include %26lt;fstream%26gt;


#include %26lt;string%26gt;


using namespace std;





int rewriteString(char *str)


{


str = "I want to output this!";


cout %26lt;%26lt; str;


return 1;


}





void main()


{


int meaninglessInteger = 0;


char str[] = "Why am I outputting this, instead of what I want to output?";


meaninglessInteger= rewriteString(str);


cout %26lt;%26lt; str;


system("PAUSE");


}





******* ******* ******* ******* ******* ******* ******* ******* *******


The above code doesn't work. I wondered if someone could help me understand what I need to change to accomplish what I'm trying to do.


I hope the code is fairly self-explanitory: I want to declare a character string in main() and then use a function called rewriteString() to change the message contained in that character string.





What's the correct way to do this? Any help is appreciated greatly!

C++: How do you edit a character string in a different function than the one you declared it in?
just my two cents ;)





int rewriteString(char *str)


{


strcpy(str,"output");


cout %26lt;%26lt; str;


return 1;


}
Reply:Okay. There is actually a very simple explanation to your answer. Once i tell you, you'll wonder why you didnt think of this in the first place.





Now, from basic C++ programming, you'd know this simple example:





void foo( int f ) { f = 25; }


void main() { int a = 10; foo(a); cout %26lt;%26lt; a; }





Now what do you think will be the output. Correct, it will be 10.


Why? Because the argument f to function foo was "passed by value" variable 'a' never changes its value. Now... just extending this to character strings.





void foo( char * f ) { f = "please please change"; }


void main() { char * t= "dont want to change"; foo(t); cout %26lt;%26lt; t; }


Now, :) i think by now you'd be saying "aaah, hmm... ". So, the solution. If you havent caught on, i'll tell you (this is Yahoo! Answers after all).. you've got two options


1. Pass parameter by reference ... void foo( char *%26amp; f )


2. Pass a pointer to the argument... void foo( char ** f )





I think you can figure out the rest. Due to the lack of availability of a compiler to check my analysis. I might be mistaken over char *%26amp; f... it might be, char %26amp;* f . But i dont think so. Sorry, but you'll have to check that.





Thanks.
Reply:I'm sorry if this is not what you want but I find it easier to just do this...





#include %26lt;iostream%26gt;


#include %26lt;fstream%26gt;


#include %26lt;string%26gt;


using namespace std;





char *rewriteString(char *str)


{


char * random = "theText";


return random;


}





int main() {


char * str = new char[100];


str = "Why am I outputting this, instead of what I want to output?";


str = rewriteString(str);


cout %26lt;%26lt; str;


system("PAUSE");


return 0;


}





===


I have no clue why you have the meaninglessInteger variable


My C++ program has only 3 error...so pl correct them and send correct code?

#include%26lt;iostream.h%26gt;


#include%26lt;conio.h%26gt;


#include%26lt;fstream.h%26gt;


const TAG_VALUE=1;


const int TAG_END_OF_STREAM=2;


class mergesort


{


private:


int a[],b[],c[10],i,j,t,k;


public:


void enter_numbers()


{


cout%26lt;%26lt;"plz enter 5 numbers"%26lt;%26lt;endl;


cin%26gt;%26gt;a[k];


cout%26lt;%26lt;"plz enter another 5 numbers"%26lt;%26lt;endl;


cin%26gt;%26gt;b[k];


}


void display_numbers()


{


c[]=a[]+b[];


for(i=0;i%26lt;=10;i++);


{


for(j=1;j%26lt;=10;j++);


if(c[i]%26gt;c[j])


{


t=c[i];


c[i]=c[j];


c[j]=t;


}


}


cout%26lt;%26lt;"The MERGE_SORTED NUMBERS were:"%26lt;%26lt;c[]%26lt;%26lt;endl;


}


};


void main()


{


mergesort m1;


fstream f1,f2,f3;


f1.open("file.dat",ios::in|ios::out|io...


while(f1)


{


m1.enter_numbers();


f1.write(%26amp;m1,sizeof(mergesort));


}


f2.open("file.dat",ios::in|ios::out|io...


while(f2)


{


m1.enter_numbers();


f2.write(%26amp;m1,sizeof(mergesort));


}


f3.open("file.dat",ios::in|ios::out|io...


while(f3)


{


m1.display_numbers();


f3.read(%26amp;m1,sizeof(mergesort));


}


f3.close();


getch();


}

My C++ program has only 3 error...so pl correct them and send correct code?
Dude.. send entire code and errors that the compiler throws up.





One error I can notice is usage of a[] Array size should be declared in definition itself; like you have done for c[10]





That style of declaration is allowed only in function calls.
Reply:1) const TAG_VALUE=1; // need a type like int!





2) c[]=a[]+b[];// no positions c[i] = a[i] + b[i];





3) cout%26lt;%26lt;"The MERGE_SORTED NUMBERS were:"%26lt;%26lt;c[]%26lt;%26lt;endl; // again missing the position.





and this might be 'while(f1)' probbably better to check for EOF, also whats with the ... never do that, its horrible.
Reply:I'd recommend doing your own homework... And a please would be nice...


Help with C++ code?

Im supposed to take a sentence ending with a '.' and determine which words have 3 or more different vowels. It should also use the isVowel function(which is below). I can determine how many vowels are in the string by the following code however i have no idea how to split up the string into words and then analyze each word nor how to determine if it has 3 DIFFERENT vowels. The program must use files. Can anyone help me. Im new to c++. Thnx.





#include %26lt;string%26gt;


#include %26lt;fstream%26gt;


using namespace std;





int isVowel(char letter)//required


{


switch(letter)


{


case 'a':


case 'e':


case 'i':


case 'o':


case 'u': return 1;break;


default: return 0;break;


}


}





void main()


{


string line;


int count=0, vowel;


char currentLetter;





ifstream data("input.dat");


ofstream results("output.dat");





while ( (currentLetter = data.get()) != EOF)


{


line += currentLetter;





vowel = isVowel(currentLetter);


if(vowel)


count++;


}





results %26lt;%26lt; line;


results %26lt;%26lt; count;


}

Help with C++ code?
Let's break it down into even smaller pieces. Splitting up a sentence into separate words can be done by writing a loop that looks for the space character.





At the moment you have a loop that reads a single character from input and checks to see if it's a vowel. You could modify this so that you read characters and store them in an array until you hit a space. When you hit the space, you process the word that is now stored in the array.





Checking for three different vowels could be done by building a very simple little data structure that records whether each possible vowel has occurred in a word. From this, it will be easy to tell if at least three different vowels have occurred.





Okay, I haven't given all the details because this is obviously an assignment, but hopefully this will get you started. Also, you should begin to see the value of breaking problems down into smaller pieces.
Reply:%26gt; Im supposed to take a sentence ending with a '.' and determine which words have 3 or more different vowels.


Use [cin.getline] function with the '.' as delimiter.


Then make a custom function to break up the sentence into words. This is accomplished using a 2d array of strings.





%26gt; It should also use the isVowel function(which is below). I can determine how many vowels are in the string by the following code however i have no idea how to split up the string into words


I recommend you go through a proper textbook [Herbert Schildt's C++: The Complete Reference is a very good and comprehensive book, and the chapter on strings is EXACTLY what you need.]





%26gt; and then analyze each word nor how to determine if it has 3 DIFFERENT vowels. The program must use files. Can anyone help me. Im new to c++. Thnx.


Once again, first you have to be thorough with the syntax. Then go through the problem in logical steps. Each 'step' becomes a function for you. [This is not Object Oriented Programming, but it doesn't really matter for this particular problem]

flower girl dresses

C++ ifstream stream question?

This is my function:


DU_parser::DU_parser(const string %26amp; filename)


{


this -%26gt; filename = filename;


ifstream in;


in.open(filename); //Line 35


...


...


...}





this is my error:


ian@ian-laptop:~/Desktop/assn5_framewo... make


g++ -W -Werror -Wall -pedantic -ansi -c -o du_parser.o du_parser.cpp


du_parser.cpp: In constructor ‘DU_parser::DU_parser(const std::string%26amp;)’:


du_parser.cpp:35: error: no matching function for call to ‘std::basic_ifstream%26lt;char, std::char_traits%26lt;char%26gt; %26gt;::open(const std::basic_string%26lt;char, std::char_traits%26lt;char%26gt;, std::allocator%26lt;char%26gt; %26gt;%26amp;)’


/usr/lib/gcc/i486-linux-gnu/4.1.2/../.... note: candidates are: void std::basic_ifstream%26lt;_CharT, _Traits%26gt;::open(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits%26lt;char%26gt;]


make: *** [du_parser.o] Error 1








Can anyone help me, please? Thanks a ton!


-Ian

C++ ifstream stream question?
Did you include fstream.h at the top of the code?


How can I use the <stream> library in c++?

Can any body tell me how how to use "fstream" in c++ to open close and manipulate files

How can I use the %26lt;stream%26gt; library in c++?
use the following links..they are useful..


first link will give you directly use of fstream to read..


similarily you can write...for that you can use help menu in your c++ package(i.e.Turbo c) you are using..


Second link will give you all details methods..to study..


In C++ what is <fstream> good for?

I've just read a tutorial about file i/o at http://www.cprogramming.com/tutorial/les...





I understand it, but I don't know what it is good for. It seems like this is just an elongated process of writing cout %26lt;%26lt; "text";

In C++ what is %26lt;fstream%26gt; good for?
cout is the output stream that displays text in a console or command prompt if it is not redirected. CGI uses it for HTTP responses. %26lt;fstream%26gt; contains classes for reading/writing files that normally are written to your hard disk or some other secondary storage.


Anythin about fstream.h in c language?

i hav encountered tat in one the project code in c language

Anythin about fstream.h in c language?
i am not getting you.


write the clear question

flower garden

Reading a file by fstream in c++?

when i open a file and readit then print contints on screen by:


while(! i.eof())


{


i.get(ch);


cout%26lt;%26lt;ch;





}


why it repeats the last charecter twice? how can i avoid that?

Reading a file by fstream in c++?
you should use





while (i.good())


{


i.get(ch);


cout %26lt;%26lt; ch;


}





you get twice the last character because the first time it returns the actual last character of the stream, then in the next iteration it triggers the EOF condition while retaining the previous value.





summing up: i.eof() is only meaningful AFTER a read operation has triggered the EOF bit.





bye


In C++ what is <fstream> good for?

I don't see why %26lt;fstream%26gt; would be used at all. It wastes a lot of space.

In C++ what is %26lt;fstream%26gt; good for?
There are 3 File I/O classes in C++ which are used for File Read/Write operations. They are





* ifstream - Can be used for File read/input operations


* ofstream - Can be used for File write/output operations


* fstream - Can be used for both read/write c++ file I/O operations


Using fstream (Visual C++)?

//File C:\1.txt contains "123456"


ifstream in ("c:\\1.txt", ios_base::in);


char c;


//reading while next char won't be eof


while(in.peek()!=char_traits%26lt;char%26gt;::eo...


in %26gt;%26gt; c;


//after that c = 6


//now moving position to begin of file


in.seekg(0, ios_base::beg);


//and starting read again


for (int i=0;i%26lt;6;i++)


in %26gt;%26gt; c;





the problem is after in.seekg(0, ios_base::beg);


curent position remains on '6'. What's the problem,


can anyone help,

Using fstream (Visual C++)?
When you enter the second input loop, the fail bit is still set from when you hit EOF the first time. Because the fail bit is set, nothing is read and c still contains 6. After falling out of the while loop, call in.clear() to clear the fail bit. Then it will work.


C++ fstream help?

Hey guys I need some different help now. When I take data from a text file, how can I dynamically bring it in as like a cstring for easy manipulation? or if thats too much to ask, how can I bring it in for manipulation when the number of items in the file isn't predefined.





Also, how can I have a user give the text file name through cin or an equivalent? I've tried just inputing a string and opening it but i get errors....

C++ fstream help?
CString...I think thats Visual C++'s class for strings?





If its a text file, you just read the data according to whatever format it's stored in. Likewise for binary (usually by records)





Inputing a filename via cin should work





char fileToOpen[256] ;


cin %26gt;%26gt; fileToOpen ;


istream input( fileToOpen ) ;


// .. etc ..

edible flowers

C++ fstream help?

First off im not even sure if the function im looking for is in the fstream library. What I'm trying to do is make a program that fetches lines from a textfile and writes them to a separat string for each line. Lets say i have a 3 line text file that looks like this





hello


whats up


nose





i want the program to write line 1 "hello" to a string, and line 2 to a different string, and so forth, how would i do this?

C++ fstream help?
You would need to declare an array of strings, and read each line into the next index.
Reply:Do you want to store each line for later processing, or do you want to just print out what's in the file, one line at a time?


C++ fstream help?

I am trying to open a file in fstream with an integer as part of the name. The integer is a variable that needs to be able to be changed. So i can open files like "1.txt", "2.txt" and so on.


Kind of what I am trying now:


fstream outfile;


int i;


for(i=0;i%26lt;10;i++)


{


outfile.open( ( i + ".txt"), fstream::app);


//do stuff to outfile


outfile.close();


}

C++ fstream help?
mapaghimagsik gives some good advice in using stringstream and normally I prefer the C++ way but sometimes falling back to the old C way is shorter.





#include %26lt;iostream%26gt;


#include %26lt;string%26gt;


#include %26lt;fstream%26gt;





using namespace std;





int main()


{


char filename[20];





fstream outfile;





for (int i = 0; i %26lt; 10; ++i)


{


sprintf(filename, "%d.txt", i);





outfile.open(filename, ios::out | ios::app);





if (! outfile)


{


cout %26lt;%26lt; "error opening " %26lt;%26lt; filename %26lt;%26lt; endl;


}





//do stuff to outfile...





outfile.close();


}


}
Reply:You need to convert i to a string to concatenate





std::stringstream str(std::stringstream::in);





std::stringstream s (std::stringstream::in | std::stringstream::out);


std::string s1;


s %26lt;%26lt; 1 %26lt;%26lt; ".txt";





s %26gt;%26gt; s1;


std::cout %26lt;%26lt; s1%26lt;%26lt; std::endl;


C++: fstream user input ?????? NEED HELP!?

Hello,





I'm trying to write a C++ program which uses input from a file. However, I want to prompt the user to choose which file they want to use as input. I have an input.txt which contains some data ( 5 integers ) and I want the user to be able to select that file for input in the program. This is where my problem arises, getting the user to choose. I've compiled the program but it doesn't work right. Here is the code:





//program to practise file input from a user





#include %26lt;iostream%26gt;


#include %26lt;fstream%26gt;


using namespace std;








struct data{





int first, second, third, fourth, fifth;


};











int main()


{


data name;


double file;





cout %26lt;%26lt; "Please input the name of the .txt file that you would like to open: " %26lt;%26lt; endl;


cin %26gt;%26gt; file;





ifstream infile("file.txt");








infile %26gt;%26gt; name.first %26gt;%26gt; name.second %26gt;%26gt; name.third %26gt;%26gt; name.fourth %26gt;%26gt; name.fifth;





cout %26lt;%26lt; name.first %26lt;%26lt; name.second %26lt;%26lt; name.third %26lt;%26lt; name.fourth %26lt;%26lt; name.fifth %26lt;%26lt; endl;





return 0;





}

C++: fstream user input ?????? NEED HELP!?
Your code is just awful.





%26gt;double file;


First of all, of you are inputing a file name, won't that be a string or a collection of characters. Double will contain numeric values. Invalid type exception.





%26gt;ifstream infile("file.txt");


if you are already asking for input from the user, why are you specifying a file name called "fie.txt". WRONG!





Well since you cant get it right, I'll as well give you the code. Since most of the errors are not too big, might be for you (*snikers*). Alrighty, maybe this will give you an idea.





#include%26lt;iostream%26gt;


#include%26lt;fstream%26gt;


using namespace std;





struct data


{


int First, Second, Third, Forth, Fifth;


};





int main()


{


data name;


char fileName[10];





cout %26lt;%26lt; "Please input the name of the .txt file that you would like to open: " %26lt;%26lt; endl;





if(cin %26gt;%26gt; fileName)


{


ifstream infile(fileName);


infile%26gt;%26gt;name.First%26gt;%26gt;name.Second%26gt;%26gt;nam...





cout%26lt;%26lt;name.First%26lt;%26lt;name.Second%26lt;%26lt;name....


}





return 0;


}





Make sure that the input be the correct file name, like "input.txt" and the inut file should have the integers followed by a white charater, like space. Example:


1 2 3 4 5 6





otherwise you'll end up with more errors.
Reply:If statement is nothing but an error checking statement. It'll protect your rest of the block i.e. in infile(), cause you can know that the actual error is just the bad input not your code. ^^ Report It