C :: Function To Return User Input
Feb 26, 2013
Firstly, I'd like to say that I'm relatively new to functions and my course standards state that I'm now supposed to relay information from the user to the program via functions, this means that I'm no longer able to use 'printf/scanf' combos in the main function.
Is it possible to do it via functions? I've tried the following method for example, but to no avail.
Code:
#include <stdio.h>
int getlowR();
int main(void)
{
getlowR();
[Code] ....
I think the following works but then lowRange is native to the function, isn't it? How can I use it outside the function? For example, how would I go about actually printing the value acquired by the function in main?
View 4 Replies
ADVERTISEMENT
Aug 5, 2014
I'm having a problem with the two while statements in my UDF.
- 1. It will run both while loops twice...?
- 2. It now goes into a continuous loop.
- 3. When it did work, it would only return 1 value to the main()...?
#include <iostream> // for use of "cin" & "cout", endl...
#include <iomanip> // for formatting setw function
#include <cmath> // for the general math computations
#include <string> // for creating descriptive strings
#include <sstream> // used to convert a string to an integer
//user defined function
int userValue (int);
[code].....
View 1 Replies
View Related
Aug 23, 2014
I have tried writing a code which takes two numbers from the user and calculates their square root then the roots are added up to return the sum. The program is coming out with loads of errors.
#include<iostream>
#include<cmath>
float main(){
using namespace std;
float m1,m2,m3,m4,m5;
[Code] ....
View 4 Replies
View Related
Nov 14, 2014
I want to return value from user defined function to main.
Code:
#include <stdio.h>
int mult ( int x, int y );
int add(int x, int y);
int loop (int y);
int main() {
[Code] ......
View 1 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
Apr 19, 2013
I tried to scanf the input by the user into the function but it does not read my input. Read on one of the thread, it said that to scanf a input into a double, need to use %1f instead to %d (which normally used. I tried changing it to %1f and it still did not work.
Code:
#include <stdio.h>
#include <math.h>
/* function main begins program execution */
int main( void )
{
double amount; /* amount on deposit */
[Code] ....
View 2 Replies
View Related
Aug 24, 2014
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);
[Code] ....
View 1 Replies
View Related
Dec 8, 2013
How to get this thing to work. All i need to do is ask user to input a name and then it brings out the line from the .txt file containing the information.
For example in my case I'm doing a member search function I'm required to ask user to input the name of the customer and then print out all the details (which consumes 1 text line in the .txt file)
Here is the code, This is the write to text file method (100% working)
Code:
cout << "Customer Name: ";
cin >> name;
// ...
ofstream myfile("customer.txt", ios::app);
[Code] .....
View 3 Replies
View Related
Mar 29, 2013
I'm writing some functions pertaining to binary trees. I've used recursion once before while learning quicksort but am still quite new and unfamiliar with it. And this is my first time touching a binary tree. So my question: In my addnode function, will the return root statement at the end ever return a value other than the value passed to the function?
Code:
#include <stdlib.h>
struct tnode
{
int data;
struct tnode * left;
struct tnode * right;
}
[code]....
View 4 Replies
View Related
Oct 31, 2014
I am studying about recursion by myself and i want to make a recursive function that prompts the user to input the base and exponent and generate the final answer .
#include<iostream>
using namespace std;
int recursive(int x, int y);
int main() {
/*int total=1;
int y, x;
[Code] .....
View 4 Replies
View Related
Nov 1, 2014
I am making program that allows the user to determine how big the array size will be and then asks the user to make up numbers to fill the array. Every time run the program on Dev C++ it says "program has stopped working"
Heres My Code:
//Assignment 19 Program 2
#include <iostream>
using namespace std;
int main()
[Code].....
View 3 Replies
View Related
Oct 10, 2014
Return the digit at the user specified index of an integer. If the integer has n digits and the index is NOT in the range 0 <=index <n return -1 Start the digit numbering at 0. Example, if user input is 4 (index) and the integer equals 123456790 the return value for the function is 5 (start index at 0) ; if user input is 40 (index) and the integer equals 123456790 the return value for the function is -1
#include <iostream>
#include <istream>
#include <cstdlib>
#include <cassert>
#include <string>
using namespace std;
int getIndex(int, int);
[Code] .....
View 4 Replies
View Related
Feb 16, 2013
This is a winsock program designed to prompt the user to enter a domain name. The program is then supposed to return to the user a resolved ip address of that domain name. The user should also be able to enter an IP address and receive back a domain name.
While the program prompts the user to enter the domain name, all that is ever returned is error#:0
HTML Code:
// Declare and initialize variables
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <windows.h>
#pragma comment(lib, "ws2_32.lib")
#include <iostream>
int main(int argc, char **argv) {
hostent* remoteHost;
[Code] ....
View 12 Replies
View Related
Apr 6, 2014
I am currently having problems creating a loop that will allow my user to choose to return to the beginning of the program or quit.
#include <iostream>
using namespace std;
int main() {
int j;
do {float a;
cout << "+----Welcome to Basic Operations----+
| Select Your Operation |
[Code] .....
I have not yet finished designing the interface for a couple of the operations, but right now i am hung up on trying to return to the beginning. I believe it is because the j was defined inside do and isn't carried out of the do statement for while.
View 13 Replies
View Related
Feb 12, 2014
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.
View 6 Replies
View Related
Jun 9, 2013
The function is supposed to return value from the file in my main, but I am getting empty value. I am trying to get better with pointer. Right now just teaching myself.
right now the only way for this code to show value is when in put the putchar(*ps) inside my readfile function. I would like to readfile to return value and print in the main function.
Code:
#include <stdio.h>
char *readfile(char filename[]);
int main(int argc, char *argv[] ) {
[Code].....
View 4 Replies
View Related
Jan 14, 2015
Why my function will not return this int. It does make it into the if(... prints "test 32" but will not return the given value(123456789).
#include <iostream>
using namespace std;
int lastZero(int x[]);
int main(){
int myArray[] = {1,1,1};
lastZero(myArray);
[Code] ...
View 4 Replies
View Related
Jan 24, 2014
I wanted to return a string I can do
Code:
string parseFilename(string fileName){
return fileName.subtr(/*blah*/);
}
Can I also use pointers/references here though? When would you use a pointer and when just a return statement?
View 2 Replies
View Related
Sep 27, 2014
I have a function that needs to return a "uint8_t" value. However before doing the processing I need to perform a test on the argument to check if it's between expected boundaries. Although this function works it gives (a logical) warning that not always a value is returned although expected. What is the normal way for functions like these where I normally should return e.g. -1 in case the test doesn't succeed and otherwise the uint8_t (t) value?
Code:
uint8_t myFunc(int a) {
if (a >= 0 && a <= 100) {
// Perform actions
uint8_t = ...
return t;
}
}
View 9 Replies
View Related
May 1, 2013
I am trying to return a pointer from a method. Below is a sample of my code.
CSubnode * CTest::GetSubNode() {
return m_psubnode;//this is declared in CTest as CSunbnode * m_psubnode
}
//in another class
m_subnode = m_ptest->GetSubNode(); //m_subnode is declared as a pointer
Is this the correct why to return a pointer?
View 2 Replies
View Related
Jul 4, 2013
How we can return a 3x3 matrix in c++ function.
My code is:
double *computeA() {
double *A = new double[2][2];
// some operations on A and then return
return A;
}
The error that I have in this case is:
error C2440: 'initializing' : cannot convert from 'double (*)[2]' to 'double *'
View 7 Replies
View Related
Aug 13, 2014
I need a function to return a string..i need to pass input as "a,b,c,a,c,d,e" function should return out put as "a,b,c,d,e".
View 3 Replies
View Related
Feb 1, 2015
I'd like a function to return either a value or the address of that value by the users input. So he can call the function like:
function("adress") - gets an adress, or function("value") - gets the value
I've tried both function overloading and templates, but none of them worked. He might input a character for the address and an int for the value... but...
Another strange thing that i observed is that the value returned by the function below is 0, so the output is address 0.
class testing
{
public:
static int x;
[Code].....
View 2 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
Nov 21, 2012
Say I have overloaded functions with different arguments AND return types. Each set of argument has only one corresponding return type.
Code:
Vector grad(Scalar)
Tensor grad(Vector)
Later I have:
Code:
template <class T>
void test(T x) {
... Y = grad(x)
}
Then how do I automatically declare the type of Y. Do I need to make the grad function a template and specialize each of them ?
View 4 Replies
View Related
Jul 6, 2013
I used rename function to rename a file.
The file names are in std::string type variable.
The function usage is like this
int result = rename (old_file_name.c_str(),new_file_name.c_str());
I expected result = 0 but i get result = -1.
Does this return code is due to any file pointer holding to this file?
View 7 Replies
View Related