C/C++ :: Basic Console Calculator - Void Function Getting Ignored?
Jun 16, 2014
I made a basic console calculator in C++ using the Code::Blocks IDE and using the GNU GCC compiler. The problem in my code is my void function called 'operate' is getting ignored by not showing up in the console. After the first if statement I added a user input by saying Y for yes or N for no. Another if statement inside the first one shows if you type either 'Y' or 'y' it will execute the 'operate' function which will restart the calculator by having the same selection menu as in the int main function. When I call the function it does not show up in the console but just ends the program.
Here is my code:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int input,input2, numAdd, numAdd2, numSub, numSub2, numMult, numMult2, numDiv, numDiv2;
void operate() {
cout << "Select a operator." << endl;
I'm trying to pass 2 arrays into a void funtion, and return values to one function.
this is the the program I'm working with, after I'm done I have to split it into 3 files, a header, a main, and a separate cpp file for the functions to live in.
#include <iostream> using namespace std; void processArrary(int numberCount[], int Numbers[], int intnumberSize, int numberCountSize); int main() { int Scores[26] = {76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167, 200, 175, 150, 87, 99, 129, 149, 176, 200, 87, 35, 157, 189}; int numberCount[8] = { 0 };
[code]...
The goal of this program is to separate and count the groups of numbers then output the amount of numbers in each group. Near as I can tell, everthing should work, but I'm getting all zeros to be displayed in each group.
error C2660: 'Add' : function does not take 0 arguments error C2660: 'Subtract' : function does not take 0 arguments error C2660: 'Multiply' : function does not take 0 arguments error C2660: 'Divide' : function does not take 0 arguments
I was reading about void as function argument, but I did not fully understand it's meaning in C.
In C++ void foo(void) {} and void foo() {}
are the same. It means no arguments for foo function. But in C it's different. First function means the same as in C++, but second means
In C, an empty parameter list means that the number and type of the function arguments are unknown. But if it is unknown you can't use this arguments if user specifies same. Because here are no variables to store them. So doesn't result are the some? You do not get any arguments. O do I can get this arguments from some hidden variable?
For example.
void foo() { printf("%d", var); } foo(5);
It is very unclear for me. Do this apply to main function too?
int main(void) int main()
or can I use arguments given to int main() like given to int main(int argc, char* argv[])
How I can let the user input a certain operator into this calculator function?. I would like to use cin to input the operator if possible because it is one of the only input commands I know.
// Calculator.cpp : Defines the entry point for the console application. // Calculates the value of two numbers based on the four main operations.
#include "stdafx.h" #include <iostream> int add(int x, int y); int subtract(int x, int y); int multiply(int x, int y); int divide(int x, int y);
I've been trying to get my program to call void functions with an if statement, but when i run my program and try to call one of the functions "worst case, best case, or random case" it doesn't get called. It just prompts the original menu.
#include<iostream> #include<fstream> using namespace std; void bubbleSort(); void selectionSort();
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;
I'm having issues with pointers and relationship operators in C.
I need to find a max and min value in a void function using pointers. max and min would work if they had values. mul works, because you can just do math operations with pointers.
There are 0 errors and warnings; but max and min are never going to work as is.
Clearly I'm missing something.
#include <stdio.h> #include <stdlib.h> void max(int *a, int *b, int *c, int *d, int *result); void min(int *a, int *b, int *c, int *d, int *result); void mul(int *a, int *b, int *c, int *d, int *result); int main()
[Code]...
Your job will be to create a program that uses pointers. Your output must be done in the main function and the calculations MUST be done in the three functions. Therefore you MUST use pointers correctly.
You must declare and implement the following 3 functions. Below are the three prototypes that you must use in this program.
void max(int *a, int *b, int *c, int *d, int *result); void min(int *a, int *b, int *c, int *d, int *result); void mul(int *a, int *b, int *c, int *d, int *result);
The functions have the following meaning:
max finds the max value of a,b,c,d and stores the largest value in result. min finds the min value of a,b,c,d and stores the largest value in result. mul multiplies a * b * c and divides by d. Stores that value in result.
Below is an example input/output. This input will be read in via the keyboard (use scanf).
input output (note that user input is shown in bold) 1 2 3 4 Enter the 4 numbers: 1 2 3 4 The max is 4. The min is 1. (a * b * c) / d = 1 100 3 201 103 Enter the 4 numbers: 100 3 201 103 The max is 201. The min is 3. (a * b * c) / d = 585
Your output MUST match exactly the output below for the input from above. Your program must compile, failure to do so will result in 0 points. */
Why does the following code compile and execute without any error? I mean, the function compareid should get 2 arguments so why does the compiler not complaining, is it because of the type of arguments?
Code: #include <stdio.h> int compareid(void* info, int value); // ansi declaration int compareid(void* info, int value)
I am new to C++ ... My question is if I were to pass some data or values from the main() function through a void function (which main purpose is to display the data in a certain manner); how should one go about doing so?
For instance:
Code: // example.cpp -- poorly written, just trying to learn #include <iostream> void format() { using namespace std; cout << "Time: " << min << hrs << endl;
[Code] .....
Obviously this is really poorly written, and confusing for the user. My main goal is to learn how to pass through values to a void function.
Also a bit off-topic, but is there a downside to place "using namespace std;" outside a function say if out of 100 functions only 10 of them use it? Would it make the program slower/unstable in any way or is this something one could do without any downsides?
The premise of this assignment is to create a menu driven, array of functions calculator. This is the code I have thus far (I haven't finished all of my comments yet, I've been to focused on clearing errors).
/*prototypes*/ void sum (int a, int b); void dif (int a, int b); void pro (int a, int b); void quo (int a, int b); void printMenu();
[Code] .....
I am using Visual Studio as a compiler, and I have the following errors:
error C2144: syntax error : 'int' should be preceded by ')' error C2660: 'sum' : function does not take 0 arguments error C2059: syntax error : ')' error C2144: syntax error : 'int' should be preceded by ')'
[Code] .....
I can only assume that I am making simple mistakes.
I have the following void function devised to assign "+1" or "-1" to each element of a matrix at random. While the function does what I want, when I print the values of the matrix all are set to zero:
#include <vector> #include "aRand.h" #include <iostream> void initConfig(std::vector<std::vector<int> > premat, int nL, int nN) { int * pnRand; pnRand = 0;
[Code]...
The function pnRand_plus returns a pointer to an array of random numbers from 1 to 100, with seed time(NULL) + i. The values printed in main are zero, despite the values printed during the function run are fine (-1s and +1s).
So I am writing this code that analyzes a file called "analysis.txt" and prints out in an ouput file (called "report.txt") the results of the analysis, i.e.,(the frequency of all alphabet letters present in the file).
I am having trouble with outputing on the file2.
here is what I have:
Code:
#include<iostream> #include<fstream> using namespace std; ifstream file1;
[Code]....
I tried to lose the "<<" after the file 2, but it's still giving me an error. how to output a void function on a text file?
using namespace std; const int SIZE = 40; const int COLUMN = 5; void getData(ifstream& inf, string n[], double tstData[][COLUMN], int count);
[Code] .....
when I compile and run the code and have it display it does not read the first item into the 1-d array, instead it appears to read the 4th number from the left into the 1-d array and then into the second spot in the 2-d array, then again in its proper place and finally it has this number repeating through the rest of the arrays:
-92559631349317830000000000000000000000000000000000000000000.00 followed by the number 59.7 from the .txt and the long number again.
i need to return a struct pointer dynamically allocated inside a function call void function() which is done using 'out parameters' in following code
struct my_struct { int x; } void my_function( my_struct** result ) { my_struct* x = new my_struct{ 10 }; //... *result = x; }
Now i have a doubt, so if i want to print the return value from struct pointer, should i need to print it in the void function() or in the caller the function...