C++ :: Call Function Syntax To Be Equivalent To Echo
Aug 19, 2014
What I want to do is simple, however I can not find a way to do it in c++ and I don't know for sure that it is possible. I want to create a function that creates a simple short-hand for printf(x); Such as:
void echo(x){
printf(x);
}
But when I call the function:
echo "hi";//I want it to look like this
//instead of:
echo("hi");// like this.
I'm familiar with function pointers as the method to pass a function as an argument to another function. However, I recently encountered some other syntax. I have seen in multiple times and in books,so I know it is probably not a syntax error. But I am not totally sure.
#include <iostream> void Function1() { std::cout << "Function1"; } void Function2(void aFunction()) { aFunction(); } // make a param that describes the function the will be given // as a param (return type, identifier,
I have a piece of code that sorts data based on some metric. The some metric is something I now want to make flexible so I can easily switch between and compare metrics. To do this, I want to pass the function to use as a parameter to the method that does the sorting (and other stuff). However, I'm having problems figuring out the syntax. Also, I think my [temporary] organization of code is violating a lot of basic code design principles.
To make the function pointer passable, I defined the "typename" in the header where the function is located (it is part of a struct, "Data"):
// Below the struct definition of Data typedef double (Data::*CostF)(unsigned l, double W) const;
The two example functions I want to use are defined in that struct ("Data"):
// Inside the struct definition inline double someExampleCost(unsigned l, double W) const { // Returns some basic calculation }
The function that uses it is part of a different class (that holds a reference to the first class, in case that matters; I feel like I'm doing something odd here, because I'm binding a member function in the definition/passing, but never referencing the object). It looks like this:
// Inside another class ("Foo") inline void DoSomeStuff(double& ECost, double& TCost, CostF cost) { // Irrelevant stuff here std::sort(vector.begin(), vector.end(), [&](unsigned a, unsigned b){ return (*cost)(a, W) < (*cost)(b, W); }); // More irrelevant stuff here }
The error shown is "operand of "*" must be a pointer". If I remove the '*': [code]return cost(A, W) < cost(b, W);
the error becomes: "expression must have a (pointer-to-)function type."
The call to this function is, currently, just in the main function, as I'm just testing before I wrap it into real code. It looks like this:
// In main Foo bar; // Make an object of the struct that has the "sorting" function CostF costFunction = &Data::someExampleCost; // Bind to the Cost function bar.DoSomeStuff(varA, varB, costFunction);
This bit shows no errors by itself. So, my questions:
a) Clearly I'm missing the insight into Function Pointers. I'm comfortable with regular pointer stuff, but I can't wrap my head around FPs, partly due to the awkward syntax.
b) I'm very uncomfortable with the fact that I'm binding a member function of a class, but never try to reference an actual object of that class. This is probably a big part of why it's not working, but I can't seem to bind a function belonging to a specific object. I thought of doing
// In the main again Data d; // Construct the object, which contains big lookup tables Foo F(d); // Construct the object, which only holds a reference to a Data object CostF costFunction = &d.someExampleCost; // Bind to the Cost function of that object
but that doesn't work ("a pointer to a bound function may only be used to call the function").
I am trying to write a terminal-like chat application in Linux. I would like to use a FIFO queue to print out the messages in terminal. The queue would be populated from 2 sources- stdin and messages sent from the other user over TCP. I have meet an obstacle that I cannot handle...
Lets say I would like to take user input using fgets and put it into a buffer. Then queue it if the buffer is not empty or print if it is. The problem is that when I use fgets or scanf, my input is instantly printed to the terminal..If i do:
Code:
fgets(message, 100, stdin); printf
("%s", message The string under message is printed twice :|. Is there a way to prevent this?
212Untitled1.cpp[Error] stray '222' in program 212Untitled1.cpp[Error] stray '' in program 212Untitled1.cpp[Error] stray '222' in program 262Untitled1.cpp[Error] stray '222' in program 262Untitled1.cpp[Error] stray '' in program 262Untitled1.cpp[Error] stray '222' in program Untitled1.cppIn function 'int main()': 2125Untitled1.cpp[Error] 'n' was not declared in this scope
error C3867: 'WordParsor::Form1::PutUpfrmIO': function call missing argument list; use '&WordParsor::Form1::PutUpfrmIO' to create a pointer to memberc:userskingc++wordparsorwordparsorForm1.h... and the suggestion fix generate another error.
One person suggested the gcroot<> object wrapper... but I do not know how to modify/declair the function or its argument type.
//This code gives randomly generated alphabets and if equal will cout the alphabet which is equal
1 #include <iostream> 2 #include <cstdlib> 3 #include <ctime> 4 using namespace std; 5 int main() 6 { 7 int a; 8 char array[10];
[Code] .....
My question is how to check that randomly generated alphabets are equal e.g in 22 number line it should give output of equal alphabets if they are equal but it does not give equal alphabets what wrong in this code mention the wrong statement, how will i get right answer?
Prompt the user to enter a day of the week as M (or m), T, W, R, F, S, and U for Monday through Sunday respectively. The user may enter an upper or lower case letter.
When the user enters a character, the program will echo the letter and output the name of the day of the week.
Provide an error trap that reads something like "you have entered an invalid letter; program aborting." Suggestion: use a switch statement with the error trap as the default condition. it is not necessary to prompt for multiple inputs.
So I know how to get the program to echo back the letter and everything. What I am a little confused about is: will I have to define all the letters as their respective day? eg. make M== Monday. And if I do have to do that how would I get it to accept Upper and Lower case letters and recognize that that letter is == monday ect. ect.
Also my main problem is the switch statement as the error trap. I have never used the switch statement, but I know what they do. I just don't really understand how I would use it for an error trap. Am I suppose to just make a case for every other letter in the alphabet other then M T W R F S and U? Even if I do that then what if the user enters a number instead of a letter?
convert a positive integer code into its english name equivalent for digit. A valid code is of size between four (4) to six (6) digits inclusive. A zero is not allowed in the code.
example : if the input is 234056 the output is : INVALID CODE (PRESENCE OF ZERO) if the input is 23456 the output is : TWO THREE FOUR FIVE SIX if the input is 9349 the output is : NINE THREE FOUR NINE if the input is 245 the output is : INVALID CODE (3 DIGITS) if the input is 2344567 the output is : INVALID CODE (7 DIGITS)
step 1 : input code step 2 : count the number of digits in the code step 3 : if there is a zero in the code, "INVALID CODE (PRESENCE OF ZERO)" go to step 4 step 4 : if number of digits is mode or equal than 4 and less or equal than 6, go to step 5 else display the following message "INVALID CODE (<number of digits> DIGITS) step 5 : call a function called digit_name to convert each digit into its equivalent english name. display the result step 6 : print the digits in reverse order eg; if input is 13453, reverse order is 35431
I've reached a point in "Jumping into C++" where I need to make a program that converts input numbers into their word equivalent.
So far I've made it work for numbers 0-9999. I've tried implementing 10000-99999 but there are problems with the order of the words printed (57865 would print fifty thousand seven thousand eight hundred sixty five). But besides that, the program is absolutely enormous (for me) and I'm wondering if it can be shortened. Keep in mind I can only use loops and if statements so far. Here it is:
This C++ question is related to Temperatures and Our task is to : Write a well-documented C program that prints out a table of temperatures in Celsius, from 0 to 100, in steps of 5 degrees, with the equivalent Fahrenheit.
To convert temperatures from Celsius to Fahrenheit use the equation : Temp(F) = Temp(C)*9/5 + 32
Include headings on the columns of figures.
Use the following formatting in your printf to produce a tabulated output:
Printf(“%4.1f %4.1f ”, Cent, Fahr);
This format will print each variable in a fieldwidth of 4 digits, with one place of decimal. The ‘ ’ inserts a tab to space the columns. This is our code, but for some reason its not working.
#include<stdio.h> void main() { int Cent=1, Fahr=1; int Temp; int F; int C;
I wrote a program which detects a pattern in an array then returns a valve (x) for each time it does. now i tried to call function patt in main so that i can print x but it doesn't let me do it.
#include <stdio.h> int patt(const int SIZE, char str[], int i, int c); int main(void) { const int SIZE=21; char str[SIZE]={'1', '0', '1', '1', '0', '0', '1', '0', '1', '0', '1', '0', '0', '0', '1', '0', '1', '1', '0', '1'}; int i, c=0;