Nick Evan commented: Holy s***, I completely missed that :( +12
StuXYZ commented: tight clean code +4
Forum glitched :/
In reply to your question bangor boy,
public void showFeelings(int howManyGoals)
{
switch (howManyGoals)
{
case 0: case 1: case 2: System.out.println("Oh dear, not very good"); break;
case 3: case 4: case 5: System.out.println("Ive seen donkeys shoot better"); break;
//and so on
}
}
Also why is your name Bangor boy, are you at Bangor, Wales or perhaps Bangors, Ireland?
Chris
as long as arraysieze contains a value when you try to allocate memory for your array yes. Here is an exmaple,
#include <iostream>
class myLameArray {
private:
int *x;
int sizee;
public:
~myLameArray(){
delete[] x;
}
void setUpArray(int size){
x = new int[size];
sizee = size;
}
void initArray(){
for(int i = 0; i < sizee; i++){
x[i] = i;
}
}
void printArray(){
for(int i = 0; i < sizee; i++){
std::cout << x[i];
}
}
};
#include "myLameArray.cpp"
using namespace std;
int main(void){
myLameArray* l = new myLameArray();
l->setUpArray(10);
l->initArray();
l->printArray();
delete l;
return 0;
}
Chris
You can define a class wide variable within your class.
class myClass {
private:
myStruct mS*;
}
Which will allow you to access mS in both functions, so you could allocate the memory to it in one function. Use it in a second function and then deallocate it in another function, such as the destructor
Chris
I dislike vsc++ but there we go. I just compiled it on MinGW with no problems what so ever, i was presented with a list of the address that your data was stored at.
if you want the data to be printed you will need to use **it not just *it.
Chris
I already gave you a solution in a C++ thread. Why do you keep cross-posting?
You cannot asign an unknown value to the size of an array at compile time. You need to use dynamic memory.
double *P = new double[N];
...
delete [] P;
For the second bit, you cannot do a mathematical equation on the left hand side of an assignment operator. It must be a variable etc.
Chris
Perhaps his real question is how can he compute the value of that series to a given figure amount using C++?
Does w3schools even mentiod Java as far as im aware its a web programming site.
Link number 5, didn't you read responses to this thread that suggested you do not use an IDE like NetBeans.
Read the sticky at the top and get started,
a good link is http://java.sun.com/docs/books/tutorial/
Also, I think that http://www.javabat.com has quite a few nice things that will get you to learn the basics of control structures etc...such as if's and loops.
Chris
Please use code tags, [CODE=cplusplus] [/code].
Firstly, is this C or C++ because it looks like a strange confusion between the two. The only header you should be using in this program is #include <iostream>
not with a .h or anything else funky.
I'd suggest you go back over the very basics of C++ before attempting to use pointers etc...since you are using global variables..which is not good.
And you obviously don't understand pointers...
Chris
Well you might want to use getline(), but the reason it doesn't work is that the ifstream constructor requires a C-Style string i.e. null terminated character array, not an object of type string. Thus you will need to use.
ifstream myfile(fil.c_str());
Chris
The other option would be to close the file stream and then re-open, btw be sure to check that you actually need to read the file stream twice, rather than loading it contence into some data structure then reading that again. Just pointing that out because file access is slow in comparison.
Chris
I think you should re-read your previous thread about creating 2D arrays witht he new operator. Look at the following two lines, If I remember correctly, I stated that to create a two dimensional array of chars such that you get a 1D array of strings, you should have the two lines that follow after the inccorect ones insead.
var=new char**[ar1];
var[x]=new char*[ar2[x]];
should be
var=new char*[ar1];
var[x]=new char[ar2[x]];
Also I would like to make a very important point. You need to free the memory you used, that means you need to learn how to use how to use delete. Not since this is a two dimensional array you will need to loop through and delete all of the elements at array[x] before delete the array with the use of delete [] array;
.
Please be very careful with allocating and deallocating memory, it leads to memory leaks that are really not wanted, and just cause more problems with code and your computer. Plus try to avoid making these mis-takes in future as some people may end up following you.
Thanks,
Chris
Are you talking about printing out the source code??
If so research Quinnes :)
Chris
Rectangle myRectangle = Rectangle();
should be Rectangle myRectangle = [B]new[/B] Rectangle();
The second too, I suggest you look at the function. Notice you declare them as taking two parameters yet you call them with 0.
Chris
Project -> Build Options -> Linker -> add -> gdi32
Chris
You need to link "gdi32.dll" into your project :)
Chris
Well you are not storing them into the array such as, a[x][y]. And your show function shouldn't be recoursive like that...it doesn't even print things out, it just cause lots of problems.
Chris
inFile >>row >>col;
This is the only line that reads data in from the file, its not looped or anything. This reads 1 integer, then skips whitespace and reads another. The fact that you don't even store them into the array at anypoint, I originally missed!
Also, you should do some error checking to make sure that the file is actually open, using
if(!inFile.good()){
cerr<<"Unable to open file.";
exit(0);
}
Why don't you have a look at something like QT. It would be a whole lot easier. But yes you could do this anyway.
Chris
Looks to me like you only read in 2 numbers from your text file. Then you go ahead and start printing out un initialised memory space....
Chris
<> state the file is located in the include directories.
"" states it may be located in the same location as the main source file.
(something like that) You need all the files in the same Project / Tell your compiler to compiler all 3 together :)
Chris
When doing this, the method is normally to have aheader file containing the function prototype. A Cpp file containing the function declaration and the a main cpp file that contains your int main(int argc, char *argv[]){}
function, etc etc.
//example.h
#ifndef EXAMPLE_H_
#define EXAMPLE_H_
int myfunc(int, int);
#endif
//example.cpp
#include "example.h"
int myfunc(int x, int y){
return (x*y)-(x+y);
}
//main.cpp
#include <iostream>
#include "example.h"
int main(int argc, char *argv[]){
std::cout << myfunc(12, 2);
return 0;
}
Chris
ostream& operator<<(ostream& os, Book& b){
cout<< "Title: " << "\"" << b.title << "\"" << endl;
cout<< "Author: " << b.author << endl;
cout<< "Genre: " << b.genre << endl;
cout<< "Publisher: " << b.publisher << endl;
cout<< "Publication Year: " << b.pubyear << endl << endl;
return os;
}
This is wrong! How do you know that when the << operator is being use it is to the cout, you should be applying it to the stream you are given 'os' you have named it! Not going straight to cout.
Please read about these things :\
What exactly is the character problem, '1' + '1' does = 'b'. What is your decode function is it not, 'b'-'1' = '1'.
Or did I miss something?
Chris
Sorry if I come across rude. Yes alot of people do it, and they also get a LOT of stick for it thats for sure.
Chris
Well, so far, nobody replied. But if I am freaking out someone I can remove it. Why is it a waste of time? And, if you consider it a waste of time what are you doing here? Are you being paid or something? :)
This is my point, we are not paid. This means we volunteer out time to help provide you with a solution in which somebody on another site is already discussing in his/her own time. Meaning the same topics get covered multiple times on different forums wasting more peoples time. No doub you will find somone such as Salem is a member of both forums, and he may well refuse to post in either, despite the fact he is perhaps one of the most valuable people to you.
If we were paid we wouldn't care
Chris
encryptedLine += (line.at(pos) + password.at(pos2));
if (pos2 > password.size()) {
pos2 = 0;
}
You try to retreive the value at pos2 when pos2 is > than password.size() before you reset pos2 to 0. Also the check should be >= or == since if the length of string is 12, then the pos2 will need to be 13 to reset, yet you only get 0...11 index on your string.
Chris
So who do you want to answer it or are you happy wasting the time of two sets of people?
Chris
OK lets goover what i've done.
#include <sstream>
#include <fstream>
int main(int argc, char *argv[])
{
std::ofstream outfile("data.txt", std::ios::in);
std::ostringstream output;
for(int count = 1; count <= 1000; count++)
{
output.str("");
output << count;
outfile << output.str() << "^" << output.str() << "+" << std::endl;
}
outfile.close();
return 0;
}
Sorry, you didn't want clear(), it was str("") you need. Anyway you can see i've made a few changes, such as removing un-needed headers, taken out namespace std;
.
Dropped your capitals name convention, 'cause it's horrible.
Then I went and decided that you don't even need stringstreams and I'm not sure why you are using them.
#include <fstream>
int main(int argc, char *argv[])
{
std::ofstream outfile("data.txt", std::ios::in);
for(int count = 1; count <= 1000; count++)
outfile << count << "^" << count << "+" << std::endl;
outfile.close();
return 0;
}
Chris
it's your string stream, you need to clear it before adding more to it.
OUTPUT.clear()
Chris
I am sure the "dic.dat" is in the same directory with the source file,
moreover, I tried the expression
if(!inClientFile.good())
but it didn't seem to work.
Lets say you are compiling with VC++ then your source code isn't in the same location as the debug or release exectuable, which means nore will "Dict.dat" be, this means you will need to move your Dic.dat file into the debug/release folders.
Chris
void *malloc ( size_t size );
This is the function prototype, and as Agni stated it returns a void pointer. Since this is the function prototype you do not use this syntax when calling the function and infact you use the syntax you originally posted. Here is a small example
int q = 34;
char *test;
test = (char*)malloc(q);
...
free( test );
The snippet above will create a character array (c-string) of length 33, since 1 space is need for the null terminator. Be sure to read about the use of free(), it's essential!
Chris
if(!inClientFile)
Please do not use this method, it is so wrong. You would be better using the following
if(!inClientFile.good())
Chris
Solution has been sent to you ... however i didnt compile the code, but it will work, i just forgot to set the boundries of array and hope that u will do it by using a nested while loop of if condition, when u try yourself.
Have a happy programming :)
Please don't PM code solutions. I suggest re-reading the forums rules too. Just to make sure you understand how we work when it comes to handing out code.
Secondly, code solutions are best in the thread they are the solution for. So in the future people are able to search for solutions to problems and find it. Rather than having to start a new topic, waste more peoples time purely because some goon wouldn't share a solution.
Chris
What do you mean by 'Text'? As in an editable text control? or just writing painted onto the windows?
there is a big difference.
Chris
all of the lines to your if statement should be wrapped in {}.
Also using cin>> is a step backwards from getline() you should read the stick "How do I clear the imput buffer?"
Chris
Sounds like you actually want something like this....
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;
int main(int argc, char *argv[]){
ifstream example("Exmaple.txt");
string line;
istringstream iss;
vector<int> part1;
vector<int> part2;
int temp1, temp2;
while(getline(example, line)){
line.replace(line.find(";"), 1, " ");
iss.clear();
iss.str(line);
iss >> temp1 >> temp2;
part1.push_back(temp1);
part2.push_back(temp2);
}
example.close();
cout << "Part 1:" << endl;
for(int i = 0; i < part1.size(); i++)
cout << part1[i] << endl;
cout << endl << "Part 2:" << endl;
for(int i = 0; i < part2.size(); i++)
cout << part2[i] << endl;
return 0;
}
istringstream iss;
iss.str("56");
int x;
iss >> x;
if(myArray[y] == x)
cout << "They match! at array position: " << y;
Post a sample of your input file and a sample of what your output should be, and what output you are getting please.
Chris
your desciption indicates that you will be given a list of random numbers that will be in no particualr order. thus you cannot just loop through the array backwards. You will need to perform some form of sorting algorithm such as a Bubble Sort
Chris
#include <fstream>
#include <string>
int main(int argc, char *argv[]){
std::string example = "Hello, world!\nHow are you?\n\tSuper thanks!";
std::ofstream exfile("test.txt");
if(!exfile.good()) return 0;
exfile << example;
exfile.close();
return 0;
}
I see no whitespace problem...
Hello, world!
How are you?
Super thanks!
Chris
You could use stringstreams to convert the string into an integer. Or you could use atoi() or even better strtol() you can research all of these on the nternet, they are all well documented, if you are still stuck then get back to us. (String streams would be the best approach!)
this line is the problem:
while (y < 5 && ids[y] != searchForId)
"searchForId" is a std::string and ids[y] is an int. You can't compare apples to pears in c++ :)
Think he is already aware of this, but doesn't know what to about chaning that
I believe that it is because I am using the string searchForId and the array is an integer
Chris
lol Year sorry, I wasn't really thinking, just stuck the numbers in without thinking :P
You should have all prototypes in the header file and the full function delarations in the external file.
Chris
Some of your header files are missing .h extensions
This is C++ not C
Infact he has extra header file that he shouldn't have such as time.h
Please try to get things reasonbly accurate before making a point of it
Chris
if (h < 0)
{
return 1; // error code +1 = underflow hours
}
else if( h > 59 )
{
return 2; // error code +2 = overflow hours
}
:)
The best way would be to use string streams
#include <sstream>
...
ostringstream oss;
oss << "\"C:\\Documents and Settings\\";
oss << rand()%1000 + 1;
oss << ".jpg\"";
system(oss.str().c_str());
Chris
I would do something like this,
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <fstream>
using namespace std;
struct cell{
int value;
string pos;
}cellData;
int main(void){
ifstream ins("example.txt");
if(!ins.good()) return 0;
vector<cell> data;
string line;
istringstream iss;
while(getline(ins, line)){
for(int i = 0; i < line.length(); i++)
if(line[i] == ';') line.erase(i, 1);
iss.clear();
iss.str(line);
iss >> cellData.value >> cellData.pos;
data.push_back(cellData);
}
for(int i = 0; i < data.size(); i++)
cout << "Value: " << data[i].value << " (" << data[i].pos << ")" << endl;
return 0;
}
I would not reccomend atoi() it's error response is poor, if you wish to do it this way I would say use strtol() (yes thats right to long, but at least it can return errors!). But I would just use string streams as you can see from above.
neik_e is correct, do not use eof() it can return true on some escpae characters so it's not advised!
Chris
Yes you would :)