C++ :: True / False About Function Parameter
Sep 11, 2014
is it true or false
a function like void myfun(int num){} can receive type "int var" but can't receive type "const int var"
AND
a function like void myfun(const int num){} can receive both type "int var" and also type "const int var"
View 3 Replies
ADVERTISEMENT
Apr 26, 2014
Write a function palindrome that takes a vector parameter and returns true or false according to whether the vector does or does not read the same forward as backward (e.g., a vector containing 1, 2, 3, 2, 1 is a palindrome, but a vector containing 1, 2, 3, 4 is not).
Code :
#include <vector>
#include <iostream>
using namespace std;
void palindrome(vector<int>);
[Code] .....
View 4 Replies
View Related
Nov 9, 2014
I was instructed to write a binary search function which would return true if an element, inputted by the user, was found in the array, and false if it was not. I'm not sure why, but my function always returns false. My code is as follows.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
//binary search function
bool search (int array[], int item)
[Code] ....
Why my code does not fulfill it's purpose???
View 7 Replies
View Related
Mar 25, 2014
I have a hit a snag in a number guessing game program. I was given a half-completed program and told to use functions to complete the missing pieces. It looks unwieldy, but this is how it is supposed to be. I have temporarily made the random guess value visible for troubleshooting purposes.
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
int welcome() {
cout << " Welcome to the hi-low game!" << endl;
[code]....
The issue lies within this piece of code:
checkGuess(guess, correct); done = false; //either true or false
} while (!done);
cout << "Congratulations, you got it!" << endl;
return 0;
}
I need to manipulate the Boolean variable done so that it registers as false when the user inputs a number higher or lower than the randomly selected value. However, if the user guesses correctly, done will become true and the program will end.
As it stands now, the program will not terminate, and if I set done equal to true like so:
checkGuess(guess, correct); done = true; //either true or false
} while (!done);
cout << "Congratulations, you got it!" << endl;
return 0;
}
Every number the user inputs will register as correct instead of the one right guess.
View 4 Replies
View Related
Oct 14, 2014
You are to write a C program that grades a true-false quiz. The quiz data will be available in a text file; here is an example:
Quiz #8 (09/22/14) (corrected version)
TTFTTFTFTF
0080 TTFTTFTFTF
0340 TTFTFFTFTF
The first line in the data file is a comment that you may assume can be up to 80 charac- ters long; it cannot be blank. The second line contains the answer key for the 10-question true-false quiz. The following lines in the data file contain a student's id number in column 1 followed by their answers for the quiz in column 2. A 0 (zero) on a line by itself indicates that there are no more students to process.
Write a program that first reads in (from standard input; more on this in a moment) the comment line and answer key as strings followed by each student's data. Store the student's id numbers in an array. You are to "grade" each student's quiz using the key provided fol- lowed by recording their scores (in a second array) in order to be able to print them out later. You do not need to use an array of strings or characters in your program. Write your program allowing for up to 100 students, and you may assume that at least 1 student will have taken the quiz.
You should create test data text files and provide the data to your program using redirection from the command line (see the sample run below). Your program should output the number of students who took the quiz, the average score, the best score, and a table showing each student's id, score, and grade. The formatting, spacing, and labels in your output should 1 match the sample run below exactly.
Your program should determine each student's grade as follows: if the score is equal to the best score b or b−1, give an A. If it is b−2, award a B. Give C's for any score that is equal to b−3 or b−4, a D for b−5 and an F for all others.
Alright So I'm just stuck at comparing the key answer with student answer here is my code comparing one student's answer with the answer key . is that right ?? One more thing for some reason when i try to print answer[0] the result is nothing why is that
Code:
#include<stdio.h>
int main(void) {
char comment[80];
char answer [10];
char studentans [10];
int n=0,i=0,x=0;
int ID[10];
[Code] .....
View 1 Replies
View Related
Jan 7, 2015
So im trying to create a quiz using c++ that incorporates three different types of questions. These are the standard one answer question, true false questions and multiple choice questions.
How to create the true false and multiple choice questions?
One way that i came up with looks like this
Question A answer1 B answer2;
View 2 Replies
View Related
Sep 28, 2014
My errors are at the end of the program in two function calls within the definition of the InsertByValue function. g++ does not seem to recognize NumArray as a valid parameter.
#include <iostream>
#include <assert.h>
using namespace std;
const int CAPACITY = 20;
/* Displays the content of an int array, both the array and the size of array will be passed as parameters to the function
@param array: gives the array to be displayed
@param array_size: gives the number of elements in the array */
void DisplayArray (int array[], int array_size);
[Code] ....
View 1 Replies
View Related
Mar 24, 2014
I have a bool type function and set it to explicitly return false, but I am still getting true as the return.
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char* argv[]){
double coeffs[3];
double roots[2];
[code].....
View 7 Replies
View Related
Apr 29, 2015
I have a text file that consists of:
1457887541
Madam
Able was I ere I saw Elba
Straw? No, too stupid a fad. I put soot on warts.
Class is cancelled today
And all of them are returning false as a palindrome and am not sure why that is so.
bool isPalindrome(const char *input){
int first = 0;
int last = strlen(input) - 1;
//begin loop to compare first position with last
while(last > first){
[Code] .....
View 3 Replies
View Related
Apr 2, 2013
I'm havin trouble outputing different false statements in a boolean function... I'm currently working on a "secret number game" program which must generate a secret number and inform the user if his/her guess number is to high, to low or correct. I know boolean return true and false.. If the number is correct, the true statement will appear, if false... THAT'S where my problem starts cause now I have TWO statements to output..In a Function.. How do I make my program able to tell if the number guessed is "too high" or "too low" ?
View 6 Replies
View Related
Oct 2, 2013
This program that I've made works fine to find midpoint, but not distance. The distancefunction always returns a 1 (true). To try to see that it wasn't the math, I added cout.setf(cout.boolalpha) to see and got a result of "true".
//This program is a start to solve basic coordinatre plane distances and midpoints
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
[Code].....
View 3 Replies
View Related
Mar 5, 2013
I am trying to write a simple program that produces different outputs based on entered age of two different users. Program should tell who is older and behave different if both users are older than 100.
Here is my program: Code: #include <iostream>
using namespace std;
int main()
{
[Code].....
Why program executes this when both users are obviously more than 100
View 8 Replies
View Related
Oct 21, 2014
The question is to write a recursive boolean function that compares two stacks and returns true if they are identical. This is where I get stuck:
If the top items of both stacks are the same, the recursive call always returns true, because 'true' is saved on top of the return stack.
Here is my code:
template<class Type>
bool identicals(stackType<Type> s1, stackType<Type> s2) {
if(!s1.isEmptyStack() && !s2.isEmptyStack()) {
if(s1.top() != s2.top())
[Code] ....
View 2 Replies
View Related
Jan 21, 2013
Write a function that takes an array and returns true if all the elements in the array are positive, otherwise, it returns false.
View 6 Replies
View Related
Jul 6, 2014
error says "cannot convert 'int*' to 'int' in function main()
and also
type mismatch in parameter in function sort(int,int)
Heres the code:
#include<iostream.h>
#include<conio.h>
void main() {
void sort(int,int);
clrscr();
[Code] .....
View 11 Replies
View Related
Oct 28, 2013
I have a class as below:
// RemoteControlMonitor.H
typedef void (*keyaction)(unsigned int key);
class RemoteControlMonitor {
private:
keyaction rph;
keyaction rrh;
[Code] .....
But I got compile error as below:
RemoteControlMonitor.H:58: invalid type `void *' for default argument to `void (*)(unsigned int)'
rcx1.C: In function `void __static_initialization_and_destruction_0(int, int)':
rcx1.C:54: ANSI C++ forbids implicit conversion from `void *' in default argument
What can I do?
View 1 Replies
View Related
Apr 17, 2014
How I can delete a parameter in a function .
int *buildTrail(int antIndex, int start, double *pheromones) {
int *trail = new int[tabu];
bool *visited = new bool[tabu];
trail[0] = start;
visited[start] = true;
[Code] ....
If I comment all lines includes visited word , no exception occurs , Otherwise , exception throws.
Simply put , How can i delete visited parameter as long as its role has been finished?
.
.
.
delete visited ;
return trail;
View 4 Replies
View Related
Jan 7, 2015
gcc v.8.3 -std=gnu++11
[URL]
I'm trying to pass a function as a parameter and failing. It seems simple, until I get the error messages.
Here is the code:
class MinimalSolver {
typedef double (*func)(double sum, double char);
void driver();
[Code]....
View 2 Replies
View Related
May 17, 2013
void foo(FILE *f1, FILE **f2) {
fputs("...", f1);
fputs("...", *f2);
} int main(void) {
FILE *fp1 = fopen(...),
*fp2 = fopen(...);
if(fp1 && fp2) {
foo(fp1, &fp2);
...
}
...
}
Forever, I've passed FILE objects into functions like the first parameter; I've never had an issue reading or writing files using that form - no file errors, no compiler warnings, etc. Recently, I saw the second parameter form, and wondered why that was?
I still don't quite get this part of pointers. What's the second parameter form doing differently than the first when the first version *appears* to work as intended??
View 6 Replies
View Related
Oct 6, 2014
How come when we pass an array as a parameter to a function, the entire array is copied and is available to function?
View 1 Replies
View Related
Jan 11, 2015
Is there any function which can return parameter list of a function.
For example : get_param(f(int x,char y )) return parameter x-> int y-> char
and f_name ->f
View 3 Replies
View Related
May 2, 2013
Write a program that inputs 10 integers from the console into an array, and removes the duplicate array elements and prints the array. By removing, I mean that you should make it appear as if the elements hadn't been there. You may assume that all the integers are between 0 and 100, Write at least 1 function in addition to the main function, and pass an array into that function as a parameter. e.g.
Please enter your 10 numbers: 1 2 3 4 5 6 7 8 9 10 The array contains: 1 2 3 4 5 6 7 8 9 10
Please enter your 10 numbers: 1 1 3 3 3 6 7 8 9 9 The array contains: 1 3 6 7 8 9
Please enter your 10 numbers: 1 1 1 1 1 1 1 1 1 1 The array contains: 1
The bolded area is where I'm having trouble. How I can go about doing this, passing an array into the function as a parameter?
Here is my code:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main () {
const int MAX = 10;
int a[MAX] = {0};
int i;
[Code]...
View 5 Replies
View Related
Dec 26, 2013
Pseudocode:
template<typename T /*, some parameter for member_function */>
class Foo {
public:
void someFunction() {
T t;
t.member_fuction(...);
} }
I'm trying to make the call to T::member_function a templated value because member_function might vary by name in my scenario. Since std::mem_fn isn't a 'type', i can't do something like Foo<std::string, std::mem_fn(&std::string::clear)> foo;
I also want to take into account that member_function might have more than one parameter. That is, the first parameter will always be something known but there might be other defaulted parameters.
The only thing I can think of is to make a proxy structure, something like this:
template<typename T, T> struct proxy;
template<typename T, typename R, typename... Args, R (T::*member_function)(Args...)>
struct proxy<R (T::*)(Args...), member_function> {
R operator()(T &obj, Args.. args) {
return (obj.*member_function)(std::forward<Args>(args)...);
} }
Which would then allow me to do (for example) this:
Foo<std::string, proxy<void(std::string::*)(), &std::string::clear> >
when Foo is implemented like this:
template<typename T, typename member_function>
class Foo {
public:
void someFunction() {
T t;
member_function()(t);
} };
That implementation works for me.
View 10 Replies
View Related
Nov 26, 2014
#include <iostream>
#include <initializer_list>
using namespace std;
void doSomething(std::initializer_list<int> list) {
} int main() {
doSomething({2,3,6,8});
return 0;
}
I write a small piece of code like above. But I can not compile it successfully. I try it both with and without the line "using namespace std", but nothing worked.
The error list: Symbol 'initializer_list' could not be resolved
I use Eclipse CDT and GCC 4.9.1.
View 3 Replies
View Related
May 2, 2013
I have been working on this all day, and its due in like an hour and a half. I have done everything the program wants except the last part. Here is the assignment:
Write a program that inputs 10 integers from the console into an array, and removes the duplicate array elements and prints the array. You may assume that all the integers are between 0 and 100, Write at least 1 function in addition to the main function, and pass an array into that function as a parameter. e.g.
Please enter your 10 numbers: 1 2 3 4 5 6 7 8 9 10
The array contains: 1 2 3 4 5 6 7 8 9 10
Please enter your 10 numbers: 1 1 3 3 3 6 7 8 9 9
The array contains: 1 3 6 7 8 9
Please enter your 10 numbers: 1 1 1 1 1 1 1 1 1 1
The array contains: 1
The bolded part is what I cant get to work. I have tried this and it keeps telling me I have not identified the items when I have.
Here is my code:
#include "stdafx.h"
#include <iostream>
using namespace std;
[Code]....
View 1 Replies
View Related
Oct 20, 2013
How can I pass a function as a parameter? I have a class that I'm trying to reuse and one of the methods in this class need to take three parameters, two ints and a function. In other words I want to be able to call a custom function every time this method is invoked when used in other classes. The function I want to call will not return any values, its a void function.
In my Class:
void Utility::someFunction(int var1, int var2, void customFunction) {
int num1 = var1;
int num2 = var2;
[Code] .....
View 9 Replies
View Related