well if you want to convert a char to an int you have to do this
char temp = '2';
int number = temp - '0'; // number is now 2
// if you do this
int number = temp; // number is now 50
well if you want to convert a char to an int you have to do this
char temp = '2';
int number = temp - '0'; // number is now 2
// if you do this
int number = temp; // number is now 50
Do you really need to use strcmp? String's have a built in operator == that you can use for comparison.
std::string foo = "foo";
std::string bar = "bar";
if (foo == bar)
std::cout << "foo and bar are equal.";
else
std::cout << "foo and bar are not equal.":
^^^ firstPerson beat me to it ^^^
you can always use the next_permutation function located in <algorithm>. As mike said you will need to break up then number into each digit and put them into some kind of container. Vectors work well for this. then you will need to sort the container to have the smallest digit at the front of the array. Then you can use a counter variable and a while statement to add up all of the permutations you can get.
int counter = 0;
vector<int> container; // put each digit here
// sort the vector
while(next_permutation(container.begin(), container.end())
{
counter++;
}
// now counter has the number of permutations
@ Nandu Das The purpose of this forum is to help not to just give you the code. If you are having a particular problem with this problem then state what that is and we can help. With that said you can do this very simply. you will need three variables and a while loop. I would use min, max and input and I would set them all to 0. Ask the user to enter a number and store that number into min and max. Then in a while loop that loops forever ask the user to enter a number or something like -999 to quit. use cin >> input;
to get the number entered by the user. Then compare input to -999. If it is equal then break the loop. if not then see if the number is less than min. if it is then set min = input. Then check to see if input is greater than max. If it is than set max = input.
The reason you had an error saying "missing ; before using" is because you were missing the ; in you other file and so it was telling you that somewhere before using you should have a ;. Try taking it out of your code now and see if it complains.
How is your matrix defined? If it is a class than you can put it into a std::list
. Also you can add a get funtion to your class that will return the element you are looking for.
class Matrix
{
//...
int GetData(int row, int col); // returns the value at [row][col]
//...
};
// in main
Matrix temp(3,3); // assuming you have a constructor like this
std::list<Matrix> matrices;
matrices.push_back(temp);
What exactly is the LNK2019 error saying?
I would suggest using an implementation like Nichito purposed.
bool valid = true;
do
{
cout << "Enter a social security number without dashes: ";
getline(cin, socialNum);
size_t size = socialNum.size(), counter = 0;
while(counter < size)
{
if(...)
{...}
else
{
valid = false;
cout << "Invalid input!\n";
break;
}
}
} while (!valid);
Yes the minimum is 4 but what do you get for output if you run this
#include <iostream>
using namespace std;
int main()
{
cout << sizeof(long int);
cin.get();
return 0;
}
I got 4 for my output.
@ stuXYZ According to my MSDN a long int and a int are the same thing? are the different with the compiler you are using? I re wrote the for loop this way to make it work with size_t and not have to use an offset.
for (size_t i = input.size() - 1; i != -1; i--)
Sorry I found what it was. Since we are going backwards I forgot size_t wont work. If you change the for loop to this it should work.
for (int i = input.size() - 1; i >= 0; i--)
What error are you getting? I am using MSVC++ 2005
Lines 21 and 22 are wrong. They should be
hasSetPages = new bool;
hasSetColor = new bool;
What exactly do you want to do if there is a non number in the string? that will determine what you need to do.
I would re write your function like this
string backwards(string input)
{
string temp;
for (size_t i = input.size() - 1; i >= 0; i--)
temp += input[i];
return temp;
}
// then in you main function
cout<<"Input backwards is : " << backwards(input);
FYI size_t is the type returned by the size() function. It is normally an unsigned int.
Okay to get the a char converted to an integer you have to subtract '0' from it. So if you have
char ch = '2';
int a = ch - '0';
Now a is 2 and not 50. What happens is '2' is 50 and '0' is 48 so 50 - 48 is 2.
@aj79 You can create you own dynamic array class for this using nothing but standard c++ keywords. You don't even need <iostream>. You should just be able you use cin.get() in a loop though for this problem. get will grab a single character at a time and you can add that to you sum. You will have to remember to subtract '0' from the char to get the actual integer.
I think the problem is coming from the way you are using your if...else statements. On line 54 you check to see if the area is greater than 750 and if it is you add your base_cost with 750 and store it into total_cost but if its not you don't add the base_cost. Try walking through your code with your debugger and see if its doing what you think it should be doing.
you could always write your own dynamic array class and use that to store the input into it then add up all of the digits. I'm not sure if that would be considered cheating since it uses the same approach as the STL.
Try wrapping the for loop in parentheses. I also split the macro into multiple lines.
#define forallXNodes(u,G) \
(for(arc *bfs_ee=(G.getSource())->firstOutArc(),arc *bfs_stopA=\
(G.getSource())->lastOutArc(),u=bfs_ee->head(); bfs_ee <= bfs_stopA;u =\
(++bfs_ee)->head()))
@ OP If your compiler evaluates both functions in this code than I suggest you get a new one.
#include <iostream>
using std::cout;
bool returnTrue ( ) { cout << "ret TRUE\n"; return true; }
bool returnFalse( ) { cout << "ret FALSE\n"; return false; }
int main()
{
if (returnFalse() && returnTrue())
cout << "toot\n";
}
Could you post the function decleration? Also the function or part of code that you are using it in.
instead of what you have on lines 118 through 126 try doing this
int counter = 1;
for (int i = 1, j = 0; i < numberUsed; i++)
{
if (entry[i] == entry[i - 1])
counter++;
else
{
store[j] = entry[i - 1];
count[j] = counter;
counter = 1;
j++;
}
}
Making a 2d array for this is pretty strait forward. Because you don't know how many duplicates are in the array you would need to have an array like
int ** twoDarray = new int*[size];
for (i = 0; i < size; i++)
towDarray[i] = new int[1];
Now all you have to do is loop through the array with a for loop that has two separate counter. One will be for the one D array and the other will be for the 2d array. Something like this
int counter = 0;
for (int i = 0, j = 0; i < size; i++)
{
//...
}
As you are going through the loop test to see if the element at oneD is the same as the previous element and if it is increment counter. If it is not than then store the counter into twoDarray[j][0], then reset counter to 0 and then increment j. This isn't very elegant but it should get the job done.
The way you have declared the templated function is wrong. if your function is a void function then instead of this
template <class T>
REGISTER_BUILDER<T>(Builder, T, "RecoBuild", ViewType::kAll3DBits);//(name, type, purpose, view)
Try this
template<class T>
void REGISTER_BUILDER(Builder, T, "RecoBuild", ViewType::kAll3DBits);//(name, type, purpose, view)
When you template a class the you have to use <...> but with functions you don't. When you actually go to use the code in you program you can write
REGISTER_BUILDER<int>(Builder, 5, "RecoBuild", ViewType::kAll3DBits);
Check this link out and you should see how it got confused. http://www.cplusplus.com/doc/tutorial/templates/
Well the main problem with starting the construction of a class from the derived part up to the base is that any variables you are using that are in your base in a function in your derived class would be undefined. Assume this situation.
class A
{
int number;
};
class B : public A
{
void foo() { number++; }
};
In this situation the compiler would have no idea what number is when it tried to compile it if it were to start at B and go to A. Since it goes the other way there is no problem.
@ oieronle
We are here to help people with there code not give them the code if they don't have any. Secondly if you are going to post code please use code tags. Third your loop wont work like you think it does. When i is 1 it gets decremented to zero and then the for loop evaluates i and comes up false because i is 0 so you never get the last digit.
Yes it is unnecessary. Didn't really think about it I just wrote what first came to mind. Thanks for pointing that out invisal. It could easily be written this way and it is probably faster.
void computeCoin(int coinValue, int& number, int& amountLeft)
{
number += amountLeft / coinValue;
amountLeft = amountLeft % coinValue;
}
I think the hint might be for the the change function and not for the user input loop. the change function you wrote could be rewritten like this
void computeCoin(int coinValue, int& number, int& amountLeft)
{
if (amountLeft % coinValue == 0)
{
number += amountLeft / coinValue;
amountLeft = 0;
}
else
{
number += amountLeft / coinValue;
amountLeft = amountLeft % coinValue;
}
}
Was the hint for how to calculate the change or for how to let the user keep entering in more numbers?
well if you want the user to be able to keep inputting a number for the change to be computed the you can wrap the whole thing in a while loop
char ch = 'y';
while (ch == 'y' || ch == 'Y')
{
// lines 17-44 here
cout << "Enter 'y' to continue 'n' to stop: ";
cin >> ch;
}
Then you should be able to make add virtual in your base class and than have another add with a different signature in your derived class
do you want to have two separate add methods in you derived class? one that takes an int and one that takes a void pointer?
probably because the compiler is using the public method from your derived class. Secondly why are you using a void * ? Third new is used to create objects on the free store. the proper syntax would be
int * number = new int;
int * numberArray = new int[50];
Just start off with a blank vector and push the objects on.
class Foo
{
// ...
}
int main()
{
vector<Foo> container;
for (int i = 0; i < 10; i++)
{
container.push_back(Foo());
}
}
You could have a newline in the input buffer that is getting extracted by the call to getline. That would give you an empty string. Try doing this.
//...
cin.ignore(80, '\n');
cout << "Enter serial number to be removed. Enter to quit:";
getline(cin, serial);
What have you tried so far?
you could also use cin.get()
to get rid of the newline.
Because there is a newline left in the buffer from the call to cin >> gender
. You will need to get it out before you call getline again. Narue has a good post about there here
I do agreee that vectors do have advantages but this is still c++. It is still usefull to know how to do these things because there are times when you would be better of with a 2d array instead of a 2d vector.
You could define it like int* spaces[3];
That should be in a separate thread. Since the question you asked has been answered then you should mark the thread as solved. Then if you have another question about the same code you start a new thread with a title that explains the problem
I would also be interested in seeing your completed code just to compare with what I did with your code. If not I understand.
If it allows you to put it in a namespace theb you might ne able to make unlink a member function and then call the namespace function inside the member function.
int ClassName::unlink(const char* foo)
{
return uinstd::unlink(foo);
}
I'm not sure if this is legal to do but maybe
namspace unistd
{
#include "unistd.h"
}
// later on
unistd::unlock();
If that doesn't work I'm not sure if it is possible but you could always change the name of your class function.
is the unlink function in the library part of a namespace?
Can you get rid of the unlink function in you class?
could you re post the code you have now.
the problem is with your set1st function. you need to set the member variables to the inputted variables. Also it should be a void function.
void set1st(int x1, int y1, char z1)
{
x = x1;
y = y1;
z = z1;
setBoard();
board[y][x] = z;
displayBoard();
}
Then in your move function you don't need to pass the variables to it just use the member variables.
void moveup()
{
if (y < 2)
{
set1st(x, y, 'X');
return;
}
set1st(x, y - 1, 'X');
}
Also you should move the system("CLS");
to right after getch(t);
in your move function. This should get you on your way.