Term does not evaluate to a function taking 1 arguments error? - c++

I wrote this code to basically shuffle a vector and I'm getting this error, and I'm not sure what's wrong. I have included algorithm. Thank you!
// Shuffle the vector
random_shuffle(names.begin(), names.end(), rand());
// Prelims
cout << "ROUND PRELIMINATION: BEGIN" << endl;
cout << names[32] << " versus " << names[29] << endl << "Please enter the winner: ";
cin >> winner;
round1.push_back(winner);
cout << endl << names[33] << " versus " << names[30] << endl << "Please enter the winner: ";
cin >> winner;
round1.push_back(winner);
cout << endl << names[34] << " versus " << names[31] << endl << "Please enter the winner: ";
cin >> winner;
round1.push_back(winner);
for (int i = 0; i < 29; i++) {
round1.push_back(names[i]);
}

The last argument to random_shuffle needs to be a function object returning a randomly chosen value. rand() evaluates to an int. Hence, it cannot be used as the last argument to the function.
The following should work.
// Shuffle the vector
random_shuffle(names.begin(), names.end(), [](int n) { return rand()%n; });

Related

Selecting an array value using a random number generator

its a text based monopoly game where i need the dice to select the number from the array like on a board.
I have the number generator, what i need to do though is when the value comes up it pluses it on the array to get the matching number so for example if the players rolls a 6, the 6 + array 0 = array value 6 which will be a name of a street but it means the player knows which place on the made up board they are on. here is the coding i am using to try and do so but i keep on getting 006ff65 what ever. i how can i get it for showing just the number as the names will be added later.
{
int main()
{
int number = 12;
int rnum = (rand() % number) + 1;
int house = 1;
int moneyscore = 10000;
double values[] = {
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40 };
char name[50];
cout << "Who are you, Dog, Car, Hat or Bus" << endl;
cin.getline(name, 50);
cout << "Welcome to Our Game " << name << " You have " << moneyscore << " .PLease Roll dice to get started" << endl;
cout << "\n----------------------Press any Enter to roll dice----------------------" << endl;
system("cls");
int choiceOne_Path;
cout << "# You roll a " << rnum << endl;
rnum = values[rnum];
cout << "# you have " << moneyscore << endl;
cout << "# You move to grid "<< values << endl;
cout << "\t >> Enter '1' Buy Property" << endl;
cout << "\t >> Enter '2' Recieve Rent" << endl;
cout << "\t >> Enter '3' End turn" << endl;
retry:
cout << "\nEnter your choice: ";
cin >> choiceOne_Path;
if (choiceOne_Path == 1)
{
cout << "\n Buy Property " << endl;
cout << " " << name << " has " << moneyscore << endl;
cout << " " << house <<" House has been placed by " << name <<" who spent 2,500" << endl;
moneyscore -= 2500;
cout << " " << name << " now has " << moneyscore << endl;
cout << "\n Roll again" << endl;
cout << "# You roll a " << rnum << endl;
}
else if (choiceOne_Path == 2)
{
cout << "\n You recieved 2500 from rent" << endl;
moneyscore += 2500;
cout << " " << name << "\n now has" << moneyscore << endl;
cout << "\n(Player will gain money form house, will need to find a way in order to make the
console remember what score == to postion)" << endl;
cout << "Ends turn" << endl;
}
else if (choiceOne_Path == 3)
{
cout << "\n Roll again" << endl;
cout << "# You roll a " << rnum << endl;
}
else
{
cout << "You are doing it wrong, player! Press either '1' or '2', nothing else!" << endl;
goto retry;
}
cout << "\n----------------------Press any key to continue----------------------" << endl;
_getch();
}
}
As far as I know, you should use srand (time(NULL)); between every call to rand() to correctly return a new random number from every call.
"srand" initialize the random number generator using a seed. In this case seed is time, which should be different on every call.
Pretty basic. You either made a few typos or need to learn how arrays work (and program flow, and subroutines, but perhaps that is for later lessons.)
First you are assigning the result of the array lookup back into your random number: rnum = values[rnum]; which is not a big deal except you use that variable later and it no longer contains what you may think it does. It actually contains the value you are looking for!
Second the variable values is a pointer to the head of your array so you are outputting the address of the values array with this line: cout << "# You move to grid "<< values << endl; there is no array look up happening at all here. It is strange you missed that because you did reference the array contents properly when you replaced the random number value earlier.

Swapping Integers using pointer

1 : http://itweb.fvtc.edu/ag/?u=3&f=cpp-assignment3
I am to swap the integer using a SwapInteger function outside the main function type using pointer. The user input a number and then the computer will compile and change the result to the given result that our professor have assigned.
I've tried creating a void swapInteger function and input some code in to see if that swap the code but that does nothing. So i just added some code into the main function but I don't think that's what our professor wanted us to do. He did stated "do not modify the main function"
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
// TODO: Implement the "SwapIntegers" function
void swapIntegers(int *first, int *second)
{
int *pSwapIntegers = first;
first = second;
second = pSwapIntegers;
}
// Do not modify the main function!
int main()
{
int first = 0;
int second = 0;
int *pFirst = new int (first);
int *pSecond = new int (second);
cout << "Enter the first integer: ";
cin >> first;
cout << "Enter the second integer: ";
cin >> second;
cout << "\nYou entered:\n";
cout << "first: " << first << "\n";
cout << "second: " << second << "\n";
swapIntegers(&first, &second);
cout << "\nAfter swapping:\n";
cout << "first: " << *pFirst << "\n";
cout << "second: " << *pSecond << "\n";
cout << "\nPress any key to quit.";
_getch();
return 0;
}
I expected the computer to compile the two integer that the user input and then show the swapped integer to the user. Please take a look at my code if you have questions
Inside of your swapIntegers(), you are swapping the pointers themselves, not the values of the variables they are pointing at. The caller's variables are not being updated.
swapIntegers() needs to look more like this instead:
void swapIntegers(int *first, int *second)
{
int saved = *first;
*first = *second;
*second = saved;
}
Also, your main() is buggy. It dynamically allocates 2 int variables that it leaks and never assigns the user's input values to. The final "After swapping" output is printing out values from those pointers, not from the variables that were actually swapped. The code will NOT display the expected output. So, despite what the instructions say, main() NEEDS to be modified in order to operate properly, and if your professor has a problem with that, tough. He made a mistake in the code he gave you.
main() should look more like this:
int main()
{
int first = 0;
int second = 0;
cout << "Enter the first integer: ";
cin >> first;
cout << "Enter the second integer: ";
cin >> second;
cout << "\nYou entered:\n";
cout << "first: " << first << "\n";
cout << "second: " << second << "\n";
swapIntegers(&first, &second);
cout << "\nAfter swapping:\n";
cout << "first: " << first << "\n";
cout << "second: " << second << "\n";
cout << "\nPress any key to quit.";
_getch();
return 0;
}
Or, like this:
// Do not modify the main function!
int main()
{
int first = 0;
int second = 0;
int *pFirst = &first;
int *pSecond = &second;
cout << "Enter the first integer: ";
cin >> first;
cout << "Enter the second integer: ";
cin >> second;
cout << "\nYou entered:\n";
cout << "first: " << first << "\n";
cout << "second: " << second << "\n";
swapIntegers(&first, &second);
cout << "\nAfter swapping:\n";
cout << "first: " << *pFirst << "\n";
cout << "second: " << *pSecond << "\n";
cout << "\nPress any key to quit.";
_getch();
return 0;
}
Or, like this:
int main()
{
int *pFirst = new int (0);
int *pSecond = new int (0);
cout << "Enter the first integer: ";
cin >> *pFirst;
cout << "Enter the second integer: ";
cin >> *pSecond;
cout << "\nYou entered:\n";
cout << "first: " << *pFirst << "\n";
cout << "second: " << *pSecond << "\n";
swapIntegers(pFirst, pSecond);
cout << "\nAfter swapping:\n";
cout << "first: " << *pFirst << "\n";
cout << "second: " << *pSecond << "\n";
delete pFirst;
delete pSecond;
cout << "\nPress any key to quit.";
_getch();
return 0;
}
UPDATE: oh wait, it is not your professor's fault, it is YOUR fault. The main() you have presented here DOES NOT match the main() given in the actual assignment!. This is what the original main() looks like:
// Do not modify the main function!
int main()
{
int first = 0;
int second = 0;
cout << "Enter the first integer: ";
cin >> first;
cout << "Enter the second integer: ";
cin >> second;
cout << "\nYou entered:\n";
cout << "first: " << first << "\n";
cout << "second: " << second << "\n";
SwapIntegers(&first, &second);
cout << "\nAfter swapping:\n";
cout << "first: " << first << "\n";
cout << "second: " << second << "\n";
cout << "\nPress any key to quit.";
_getch();
return 0;
}
This code is correct. So YOU are the one who introduced the bad use of pointers in main(). So just revert back to the original main() code you were given. And then implement swapIntegers() properly. As the instructions told you to do.

I am trying to use a do while loop to repeat a certain portion of my program and it refuses to execute properly

Okay so as the title said its refusing to execute the stuff right under the "do" function even though as far as i can tell all the parameters for a repeat have been fulfilled. So far what i get when i run the program is something along the lines of...
"Would you like to search another name?
Please enter Y for yes and n for no:"
looping over and over when i press y
#include <iostream>
#include <string>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <cstdlib>
using namespace std;
int main()
{
vector <string> vName, vID, vClass;
string sName, sID, sClass, sSearch, cQuestion;
int iSize, iStudent;
// Display initial vector size
iSize = vName.size();
cout << "Student list starts with the size:" << iSize << endl;
// Get size of list from user
cout << "How many students would you like to add?" << endl;
cin >> iStudent;
cin.ignore();
// Get names, ids, and classes
for (int i = 0; i < iStudent; i++)
{
cout << "Student" << i + 1 << ":\n";
cout << "Please enter the student name: ";
getline(cin, sName);
vName.push_back(sName);
cout << "Enter ID number ";
getline(cin, sID);
vID.push_back(sID);
cout << "Enter class name ";
getline(cin, sClass);
vClass.push_back(sClass);
}
// Display header
cout << "The list of students has the size of: " << iStudent << endl;
cout << "The Student List" << endl;
cout << "\n";
cout << "Name:" << setw(30) << "ID:" << setw(38) << "Enrolled Class : " << endl;
cout << "--------------------------------------------------------------------------";
cout << "\n";
// for loop for displying list
for (int x = 0; x < vName.size() && vID.size() && vClass.size(); x++)
{
cout << vName[x] << "\t \t \t" << vID[x] << "\t \t \t" << vClass[x] << endl;
}
// Sorting function
cout << "\n";
cout << "The Student List after Sorting:" << endl;
cout << "\n";
sort(vName.begin(), vName.end());
for (int y = 0; y < vName.size(); y++)
{
cout << vName[y] << endl;
}
cout << "\n";
// Search function
do
{
cout << "Please Enter a name to be searched:" << endl;
getline(cin, sSearch);
if (binary_search(vName.begin(), vName.end(), sSearch))
{
cout << sSearch << " was found." << endl << endl;
}
else
{
cout << sSearch << " was not found." << endl << endl;
}
cout << "Would you like to search another name?" << endl << endl;
cout << "Please enter Y for Yes and N for No:" << endl << endl;
cin >> cQuestion;
} while (cQuestion == "Y" || cQuestion == "y");
cout << "Thank you for using this program!" << endl;
return 0;
}
Edit:
Posted whole program, please excuse any grammatical mistakes, I'm just trying to get the program down before i go in there and make it pretty.
The tail of your loop does this:
cout << "Please enter Y for Yes and N for No:" << endl << endl;
cin >> cQuestion;
which will consume your string if you entered one, but leave the trailing newline in the input stream. Thus when you return to the top of the loop after entering Y or y, and do this:
cout << "Please Enter a name to be searched:" << endl;
getline(cin, sSearch);
the getline will extract an empty line.
How to consume the unread newline from the input stream is up to you. You will likely just end up using .ignore() as you did prior in your program. Or use getline to consume cQuestion. You have options. Pick one that works.
And as a side note, I would strongly advise you check your stream operations for success before assuming they "just worked". That is a hard, but necessary, habit to break. Something like this:
do
{
cout << "Please Enter a name to be searched:" << endl;
if (!getline(cin, sSearch))
break;
if (binary_search(vName.begin(), vName.end(), sSearch))
{
cout << sSearch << " was found." << endl << endl;
}
else
{
cout << sSearch << " was not found." << endl << endl;
}
cout << "Would you like to search another name?" << endl << endl;
cout << "Please enter Y for Yes and N for No:" << endl << endl;
} while (getline(cin,cQuestion) && (cQuestion == "Y" || cQuestion == "y"));
If cQuestion is a char array then you need to use strcmp or stricmp to compare it with another string i.e. "Y" and "y" in this case. If cQuestion is a single char then you need to compare with 'Y' and 'y' (i.e. with a single quote)
Strings in C++ are not first class types therefore they do not have some of the string operation that exist for other basic types like ints and floats. You do have std::string as part of the standard C++ library which almost fulfills the void.
If you just change the type of cQuestion to std::string your code should work but if you want to stick with chars then you will need to change the quote style.

Check for non-numeric inputs in a C++ program

How do you check for non-numeric input using C++? I am using cin to read in a float value, and I want to check if non-numerical input is entered via stdin. I have tried to use scanf using the %d designator, but my output was corrupted. When using cin, I get the correct format, but when I enter, a string such as "dsffsw", I get an infinite loop.
The commented code was my attempt to capture the float, and type cast it as string, and check if it is a valid float, but the check always comes up false.
I have tried using other methods I have found on the message boards, but they want to use scanf in C and not cin in C++. How do you do this in C++? Or in C if it is not feasible.
while (!flag) {
cout << "Enter amount:" << endl;
cin >> amount;
cout << "BEGIN The amount you entered is: " << strtod(&end,&pend) << endl;
//if (!strtod(((const char *)&amount), NULL)) {
// cout << "This is not a float!" << endl;
// cout << "i = " << strtod(((const char *)&amount), NULL) << endl;
// //amount = 0.0;
//}
change = (int) ceil(amount * 100);
cout << "change = " << change << endl;
cout << "100s= " << change/100 << endl;
change %= 100;
cout << "25s= " << change/25 << endl;
change %= 25;
cout << "10s= " << change/10 << endl;
change %= 10;
cout << "5s= " << change/5 << endl;
change %= 5;
cout << "1s= " << change << endl;
cout << "END The amount you entered is: " << amount << endl;
}
return 0;
}
int amount;
cout << "Enter amount:" << endl;
while(!(cin >> amount)) {
string garbage;
cin.clear();
getline(cin,garbage);
cout << "Invalid amount. "
<< "Enter Numeric value for amount:" << endl;
}
I think you task relates to the so called defensive programming, one of it`s ideas is to prevent situations like one you described (function expects one type and user enters another).
I offer you to judge whether input is correct using method that returns stream state , which is good(),
so I think it will look something like this:
int amount = 0;
while (cin.good()) {
cout << "Enter amount:" << endl;
cin >> amount;

addition operator has no affect

I am trying to add two floats in a for loop and its telling me '+' has no affect. I am attempting to make it parse through each incrememnt (.25) of the two ranges (begrate and endrate) (1 and 2) and 1+.25 is not working correctly and I get an infinite loop
float begrate,endrate,inc,year=0;
cout << "Monthly Payment Factors used in Compute Monthly Payments!" << endl;
cout << "Enter Interest Rate Range and Increment" << endl;
cout << "Enter the Beginning of the Interest Range: ";
cin >> begrate;
cout << "Enter the Ending of the Interest Range: ";
cin >> endrate;
cout << "Enter the Increment of the Interest Range: ";
cin >> inc;
cout << "Enter the Year Range in Years: ";
cin >> year;
cout << endl;
for (float i=1;i<year;i++){
cout << "Year: " << " ";
for(begrate;begrate<endrate;begrate+inc){
cout << "Test " << begrate << endl;
}
}
system("pause");
return 0;
That's because begrate+inc has no effect on the value of begrate. The + operator is not like the ++ operator. You must assign the results to something to have an effect. What you wanted is this:
begrate = begrate + inc
Or
begrate += inc
You could use += instead of +, as this will set begrate to begrate+inc. The better solution would be to have a temporary loop variable that starts equal to begrate then increment it.
for (float i=1;i<year;i++){
cout << "Year: " << " ";
for(float j = begrate;j<endrate;j+=inc){
cout << "Test " << j << endl;
}
}
Just replace the following line
for(begrate;begrate<endrate;begrate+inc){
with
for(begrate;begrate<endrate;begrate+=inc){
notice the begrate*+=*inc here