Can you post the code where you make the board and all its contents?
ghost_from_sa commented: works :) +1
pcgamer2008 commented: Yeah ! nice ..good idea for time based game projects +0
Can you post the code where you make the board and all its contents?
It looks like the problem is with your outer for() loop because you are starting from 7 and going down to 1 (since its >0). Index 7 is out of range and 0 is used so you will be missing information and getting junk info.
#include <iostream>
using namespace std;
int main()
{
int sheepCount = 0;
char board[][7]= {{' ',' ','S','S',' ','S',' '},
{' ',' ','S','S',' ','S',' '},
{' ',' ','S','S',' ','S',' '},
{' ',' ','S','S',' ','S',' '},
{' ',' ','S','S',' ','S',' '},
{' ',' ','S','S',' ','S',' '},
{' ',' ','S','S',' ','S',' '}};
for( int i = 0; i < 7; i++ )
{
for( int j = 0; j < 7; j++ )
{
if( board[i][j] == 'S' )
{
sheepCount++;
}
}
}
cout << "sheepCount -> " << sheepCount << endl;
return 0;
}
Does it even need a return? I ran the code that I posted and it works fine. Just a bunch of warnings.
And you just posted on a thread that is over 1 month old.
The solution is in the if() statement where it prints either a * or space.
You should use the C++ headers if you are using C++ ( you were using iostream.h instead of iostream ).
And you should practice formatting your code so it is easier to read because you have cout << "\n"; tabbed out by cout << " "; which kinda looks confusing.
#include <iostream>
using namespace std;
int main()
{
int a, b, c = 5, d = 0;
for( a = 1; a <= c; a++ )
{
for( b = 1; b <= c*2-1; b++ )
if( (b == c+d || b == c-d) || a == c )
cout<<"*";
else
cout<<" ";
cout<<"\n";
d++;
}
return 0;
}
Not sure if tabs and newline characters count but its been trimmed a bit too
#define X scanf("%d",&
main(s,n,t){for(X t);t--;){X n);s+=n;}printf("%d",s-1);}
I was wondering why you called it CSV since there wasnt a comma in it. Then I refreshed the page a few times and you edited your post! I have no idea how you were kinda thinking you wanted the data to be stored so I made a DATA structure to hold the strings and numbers.
I'm sure you can think up a better way to output the data if you wanted to.
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
using namespace std;
struct DATA
{
string day, month;
int date;
string time, timezone;
int year;
void Display()
{
cout << day << " " << month << " " << date << " " << time << " " << timezone << " " << year;
}
};
int main()
{
string line;
vector<DATA> DataStore;
ifstream file("test.txt");
while( getline(file,line, ',') )
{
stringstream lineStream(line);
DATA element;
lineStream >> element.day >> element.month >> element.date >> element.time >> element.timezone >> element.year;
DataStore.push_back(element);
getline(file,line);
}
for( unsigned int i = 0; i < DataStore.size(); i++ )
{
DataStore[i].Display();
cout << endl;
}
file.close();
return 0;
}
Can you post a sample input? I cant visualize this at all.
This is what I'm seeing
abc123,12345
def456,94032
....
Is that not what you asked above?
How can I store the data from column one only?
In your while() loop condition that actually pulls a line out of the file and stores it into line so you are reading every second line with your code. Also you are overwriting your holding variable bit with the 2nd column of information.
The only part that is really different below is the while() loop. Other than that I think its pretty much the same.
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main()
{
string line;
vector<string> DataStore;
ifstream file("test.txt");
while( getline(file,line, ',') )
{
DataStore.push_back(line);
getline(file,line);
}
for( unsigned int i = 0; i < DataStore.size(); i++ )
cout << DataStore[i] << endl;
return 0;
}
I just said post your code and NOT just assignment #3.
Funny thing is that you just copy pasted your assignment without even posting assignment #3 which is referred to in this one.
Post what you have so far (and not the original assignment #3) and say what you need help with.
When you are giving people code and you want them to read it you should make sure your formatting is consistent. Some of your {} are to the far left when they should be tabbed in once because it makes it harder to see what is going on with the code when you can hardly read it.
Anyways your program seems to work fine other than the fact that it always says you entered an invalid option unless you select 5. This is because you have a bunch of if statements in series when you should have an if followed by else if's or just a switch statement.
switch (option)
{
case 1:
calcShape();
break;
case 2:
calcCircle();
break;
case 3:
calcRightTriangle();
break;
case 4:
calcEqualTrianle();
break;
case 5:
calcCylinder();
break;
default:
cout << "That is not a valid operation geocalc will now close" << endl;
break;
}
For the first two errors make sure that you either have both of the classes above Point2D class or just put the class prototypes above the Point2D class
class aRectangle;
class Offset;
As for the 3rd and 4th errors (4th is caused by the 3rd) you probably do not have a Offset variable declared within main().
The previous question is a messy mixture of OO and non-OO code. Tidy up the cody by:
1) implement class Offset and use an offset object as an argument to the member function MoveRelative(). An offset object has two values that are added to the x and y vlaue of a point.
2) Implement class aRectangle and use an aRectangle object as an arguement to the contains method
To me this does not sound like he wants you to use friendship/inheritance.
I don't think you have to use any inheritance for the aRectangle class like your last post says.
And the way I set this up I'm not too sure why he got you to use classes for Offset and aRectangle, unless he wants you use get() and set() functions to assign values, when you could just use structures since they start off being public (opposed to classes which start out private).
Point2D.h changes/additions
class Offset
{
public:
Offset(int x = 0, int y = 0)
{
_x = x;
_y = y;
}
int _x, _y;
};
class aRectangle
{
public:
aRectangle(int x1 = 0, int y1 = 0, int x2 = 0, int y2 = 0)
{
_x1 = x1;
_y1 = y1;
_x2 = x2;
_y2 = y2;
}
int _x1, _y1, _x2, _y2;
};
//changes within the Point2D class
void moveRelative(Offset oset)
{
x += oset._x;
y += oset._y;
}
int contains(aRectangle rect)
{
if (x <= rect._x1 && x >= rect._x2)
{
if (y <= rect._y1 && y >= rect._y2)
return 1;
else
return 2;
}
else if (y <= rect._y1 && y >= rect._y2)
{
if (x <= rect._x1 && x >= rect._x2)
return 1;
else
return 3;
}
return 0;
}
main.cpp changes/additions
int main(int argc, char* argv[]) //might look different because I use code::blocks and it doesn't like your format (just change it back)
{
Point2D aPoint; //unchanged
aRectangle rect(8,6,4,2); //declare a rectangle
Offset oset; //declare an offset
int …
Like abhimanipal said go check out that website because it is really useful
Here is something I made to show how to read in, sort, then output some data from a text file to a text file.
//#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
struct PERSON
{
string name;
int number;
};
bool cmp(PERSON a, PERSON b) //used for sorting
{
if( a.number > b.number )
return false;
return true;
}
int main()
{
vector<PERSON> people; //holds the data read in
ifstream in; //creates an in file stream
in.open("input.txt");
PERSON tempGuy; //holds data being read in
while(in >> tempGuy.name)
{
in >> tempGuy.number;
people.push_back(tempGuy);
}
in.close(); //closes the stream when done with it
ofstream out; //creates an out file stream
out.open("output.txt");
sort(people.begin(), people.end(), cmp); //sorts the people based on number
vector<PERSON>::iterator it;
for( it = people.begin(); it != people.end(); it++ )
out << (*it).name << " " << (*it).number << endl; //outputs people to text file
out.close(); //close when done
return 0;
}
"input.txt"
jim 10
john 32
bob 22
sean 54
mark 49
steve 27
justin 35
alan 31
scott 19
stew 42
The problem is because you are setting your "destination point" on line 26 to invokeX/Y (0,0) and getting the distance from the invoke point (0,0). So either I would delete line 26 and replace destinationPoint on line 35 with pt1.
Here is something I threw together using the code you posted. This is what I think you are trying to do.
Since the distance function is within your class I used the point within the instance with the point passed into the function to determine the distance rather than hard coding 0 (I know that you just did that for testing).
#include <iostream>
#include <cmath>
using namespace std;
double sqr(double x) //made this because I hate using the pow function for powers of 2
{
return x*x;
}
class Point
{
public:
Point();
Point(const double x, const double y);
//Point(Point &otherPoint); //not sure why you are passing reference but left this one out
double getX();
double getY();
void setX(const double X); //undefined/unused in this code
void setY(const double Y); //undefined/unused in this code
void print(); //undefined/unused in this code
double distance(Point destinationPoint);
private: //just fyi this can be up at the top since classes start off private
double _x; //put _'s before the variable name because you
double _y; //had the constructor with the variable names
};
Point::Point()
{
_x = 0;
_y = 0;
}
Point::Point(const double x, const double y)
{
_x = x;
_y = y;
}
double Point::getX()
{
return _x;
}
double Point::getY()
{
return _y;
}
double Point::distance(Point destinationPoint)
{
return sqrt(sqr(destinationPoint.getX() - _x) + sqr(destinationPoint.getY() - _y));
}
int main()
{
Point invoke(0,0);
Point pt1(2,2);
cout << "The distance from invoking point (" << invoke.getX() << "," << invoke.getY() << ")" …
Above you have aPoint being passed into the distance function and you are displaying pt1's x and y values. Do you mean to pass pt1 into the function? Based on the code that you haven shown I cannot see anything else that could be causing the problem.
A simple way to think about it is that GL_PROJECTION is to camera and GL_MODELVIEW is to world objects.
So if you use glTranslatef() in projection mode then you will be moving your viewing position or camera. And if you translate in modelview then you are moving the drawing point for objects.
glLoadIdentity() just takes the view that you passed into glMatrixMode() and loads it as the current matrix.
Here is the timer class.
It is pretty easy to use. You just have to initialize a timer then set the delay using setDelay(x) (where is is in milliseconds) and then use start() to start it up. Then use checkDelay() to check to see if the timer is up or not and if it is then use restart() to reset the timer so you can delay your next motion.
There are a few things in here that you might not need to use such as the setCD() function because it was made for some game using ability cooldowns.
timer.h
#ifndef TIMER_H
#define TIMER_H
class TIMER
{
int startTicks;
int pausedTicks;
bool paused;
bool started;
int delay;
int timeBuffer;
public:
TIMER();
void start();
void stop();
void pause();
void unpause();
void restart();
int getTicks();
bool isStarted();
bool isPaused();
int getDelay();
void setDelay(int delay = 1000);
void setCD(int delay = 1000);
int getSecondsLeft();
bool checkDelay();
};
#endif
timer.cpp
TIMER::TIMER()
{
startTicks = 0;
pausedTicks = 0;
paused = false;
started = false;
timeBuffer = 100;
}
void TIMER::start()
{
if(started == false){
started = true;
paused = false;
startTicks = clock();
}
}
void TIMER::stop()
{
started = false;
paused = false;
}
void TIMER::pause()
{
if( ( started == true ) && ( paused == false ) )
{
paused = true;
pausedTicks = clock() - startTicks;
}
}
void TIMER::unpause()
{
if( paused == true )
{
paused = false;
startTicks = clock() - …
I'd say the best way to learn is by trying to draw it out on paper with all the vertices labeled and make all the basic shapes outta quads to start with or jump use triangles if you feel a bit braver.
You can make a timer class and have it check to see if it is past your time delay between movements. If it is then add the offset then draw otherwise just draw.
I have a timer class if you would like to see/use it (its pretty basic).
You have to tell the fstream object that you want to output with it by typing:
fstream out;
out.open("output.txt", fstream::out);
You can go into your project settings and make it a Win32 Windows application and that will hide the console window.
If you want to check all the "boids" against each other but not themselves then you can use this.
int main()
{
//BOID boid[3]; no idea what data type/structure you are using
for( int i = 0; i < 3; i++ ) //the boid index you want to compare to the others
for( int c = 0; c < 3; c++ ) //the boids being compared to
if( i != c ) //if the indices do not match (not the same boid)
{
//do your distance check/setting color in here
/*
if( boid[i].IsInRangeOf(boid[c]))
boid[i].SetColor("Red");
*/
}
return 0;
}
I put in some functions that are pretty much just there to tell you where you can do your changes but I have no idea how you have set your program up.
I see the problem at line 33 of your first post.
Replace that line with
vector<User> &getUsers() {return users;}
The problem is that you are getting a copy of the user then you are modifying that but not the original copy of it.
The code I posted below shows two functions, one using global variables and the other using reference to variables, that could be used for keeping track of the score.
If it is a small game I would just use the global variables since you do not have to fill out the function parameters every time you want to use it. But I would still play around with the second option because pointers and references are very useful and can clean up code lots.
#include <iostream>
using namespace std;
int win1 = 0, loss1 = 0; //globals
void scorecounter( bool playgame, bool playagain ) //uses the global variables
{
if( playgame == false )
loss1++;
else if( playgame == true )
win1++;
if( playagain == false )
cout << "Wins: " << win1 << " Losses: " << loss1 << endl;
}
void scorecounter( bool playgame, bool playagain, int &win, int &loss ) //uses reference to variables in main()
{
if( playgame == false )
loss++;
else if( playgame == true )
win++;
if( playagain == false )
cout << "Wins: " << win << " Losses: " << loss << endl;
}
int main()
{
int win2 = 0, loss2 = 0;
scorecounter( true, false );
scorecounter( true, false, win2, loss2 );
cout << win2 << " " << loss2 << endl; //this shows that the variables have been changed by the function above
return 0;
}
A class is made up of functions and variables so you can not randomly throw in a function call like srand() unless it is within one of it's own functions. I would call srand() at the top of your main() function.
#include <iostream> //use iostream not iostream.h (.h is the c version)
#include <ctime>
using namespace std;
class Horse
{
int position;
int random;
public:
Horse(){}; //note the two curly brackets (this is the default constructor that is made if you do not include a constructor)
void advance()
{
random = rand() % 2;
cout << random;
}
int getPosition( int y )
{
return y;
}
};
int main()
{
srand(time(0));//seed your random here.
Horse test;
test.advance();
return 0;
}
I have never seen anything like this but this gives the needed output.
#include <iostream>
using namespace std;
int main()
{
if( cout << "aa", 0)
cout << "aa";
else
cout << "dd";
return 0;
}
This is a basic version of what I think you are trying to do.
#include <iostream>
using namespace std;
class DRAGON
{
public:
int hp;
int def;
int atk;
bool alive;
DRAGON();
int getHealth();
void setHealth(int);
};
DRAGON::DRAGON()
{
hp = 50;
def = 2;
atk = 10;
alive = true;
}
int DRAGON::getHealth()
{
return hp;
}
void DRAGON::setHealth( int dmg )
{
if( hp - dmg < 0 )
hp = 0;
else
hp -= dmg;
}
void attackPTR( DRAGON *d1, DRAGON *d2 )
{
d1->setHealth(d2->atk - d1->def);
d2->setHealth(d1->atk - d2->def);
}
void attackREF( DRAGON &d1, DRAGON &d2 )
{
d1.setHealth(d2.atk - d1.def);
d2.setHealth(d1.atk - d2.def);
}
int main()
{
DRAGON d1, d2;
attackPTR( &d1, &d2 ); //using pointer attack function
cout << "d1: " << d1.getHealth() << " d2: " << d2.getHealth() << endl;
attackREF(d1, d2); //using reference attack function
cout << "d1: " << d1.getHealth() << " d2: " << d2.getHealth() << endl;
return 0;
}
I put in both pointer and ref attack functions to show that they do the exact same thing but by reference makes it so you dont have to keep putting in the arrows and you do not have to pass by reference in the function usage.
I would try to use your code to show you an example but you are showing code that doesnt have to be posted for your problem and now showing parts that are key to finding the error.
And by the looks …
void sethpmax(){hpmax = lvlhp[lvl - 1];};
void setexpmax(){expmax = lvlexp[lvl - 1];};Random arrays assigned to int? This should not even compile.
This looks fine to me. Hes assigning the units health to whatever is at lvlhp[lvl-1] is. So if the guy is level 5 it would be lvlhp[5-1] or lvlhp[4]. That makes perfect sense.
It will only output the last letter because a char type is only one character. You can use a string and add the letters onto that then output the word with outFile.
Put one do()while() within the other do()while() so it keeps looping unless you are done your game loop and when you do not want to play again.
Doing this you will want to have two variables instead of having both as done.
You are missing return 0; at the bottom of your main function.
Without compiling I see on line # 9 you have used T[size) instead of T.
Put a condition in where it will only output if it is not the last element or break outta the loop if it is on the last element instead of displaying a comma.
#include <iostream>
using namespace std;
int main()
{
int myArray[] = {1,2,3,4,5,6,7};
int szArray = sizeof(myArray)/sizeof(int);
cout << "\nArray: {";
for( int i = 0; i < szArray; i++ )
{
cout << myArray[i];
if( i != szArray-1 )
cout << ", ";
}
cout << "}";
system("PAUSE");
return 0;
}
I honestly feel like you arent learning lol. Thats a really basic part of programming. You are passing a variable into a function and doing operations on then returning a value. You are passing 100 into it and that is returning 5.0 because if you read inside 100 outputs 5.0. You want it so that your input is ran into the function so you go get the input from the user and save it to the variable grade. Then you want to pass grade into the function to get an output. To answer your question you have to replace 100 with grade. If I have to tell you this then all I gotta say is I hope you arent in computer science because you will be retaking this class.
Because on line 12 you have hardcoded the input 100. Replace that with the grade variable.
Put the pause between line 13 and 14. That will pause the program right at the very end so you get to see all the outputs.
post your entire program, in code tags lol
yeah, sorry for the late reply I'm guessing you have already tried.
Its because you have no way to make the program "wait" before it closes.
On windows you can use system("PAUSE");
or you can use cin.get();
for a pause at the end of your program
#1 if you look at the code I posted I spent a bit of time formatting it so that it is pretty easy to read. You need to learn how to use tab/indents otherwise when someone reads your code it will be very difficult for them to figure out what is going on.
The way you have validGrade() right now it will always return true because the way I had it set up for you was like this.
bool validGrade( int in )
{
if( in >= 0 && in <= 100 )
return true;
return false;
}
Notice that in is the variable used to store the variable that was passed into the function. You are using the name of the function for some reason and that is wrong. You check to see if the grade is 100 output some stuff and then right when the if ends it returns true without ever having a condition where it could return false. The code I posted above shows that it checks to see if the grade is between 0 and 100 -- if this is true then return true otherwise it wont run what is inside the if statement and it will just return false.
In your convertGrade() function you have kinda the right idea other than the fact that you are not testing against a variable so you would have to put if( in >=90 && in <= 100 )
to get the desired result. Anything …
Did you throw in a cout << y << endl;
at the end?
lines 21 and 22 are from your original code. Delete them if you do not want to see your inputs every time.
Post what you put in. I tested this before I posted.
You can use the accumulate function like jonsca said or you can just use a for() loop and add them all up. Since you didn't know how to add a vector up I would recommend using this method so you can figure out what is going on then maybe use the accumulate function later.
#include<iostream>
#include<vector>
using namespace std;
typedef vector<int> VECTORINT;
int main()
{
VECTORINT Vectorul;
int x;
int num;
int y = 0; //sum?
cin >> num; //the number of numbers that must be entered
for( int i = 0; i < num; i++ )
{
cin >> x;
y += x; //either add them here
Vectorul.push_back(x);
for( int j = 0; j < Vectorul.size(); j++ ) //do not nest for() loops with the same variable
cout << Vectorul[j] << endl; //seems kinda pointless and just clutters the input/output
}
for( int i = 0; i < Vectorul.size(); i++ )
y += Vectorul[i]; //or add them here BUT do not include both of these sections
system("pause");
return 0;
}
You could just create an int called sum with an initial value of 0 and then make a for() loop just add all the elements to sum one by one until the end of the vector.