C++ :: Putting Variable In A Function And Getting Return Value?
Dec 25, 2014
I want to get a random number from this function but idk if I did this function right....
int random(int x) {
srand(static_cast<unsigned int>(time(0)));
int randomN = rand();
x = (randomN % 10) + 1;
return x;
}
View 5 Replies
ADVERTISEMENT
Feb 7, 2014
I am making a function that will return a pointer to a long long variable. For example, I have the next variable prototype: Code: long long funcName(long long x, int s); I want to change the return value, and the first parameter to pointers to long long.
View 4 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
Nov 21, 2014
So I have been working my way through this assignment and I'm just stuck now. I cannot get this work properly It just tells me I'm trying to return a local variable when attempting to return postfix in the to_postfix function. It is line 97 that wont compile. Also I cannot change anything in runner.c.
Calculator.c
Code:
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <ctype.h>
4 #include <string.h>
5 #include "stack.h"
[Code] .....
View 11 Replies
View Related
Jun 1, 2014
Suppose i have a function:
int & f1(int & p) {
return p;
}
void main() {
int a,b;
b = 10;
a = f1(b);
a = 11;
cout<<b<<endl;
}
why when i change a, b doesnt change? a is supposed to be an alias of b right?
View 5 Replies
View Related
Mar 20, 2013
I need to create a loop to return a minimum value using a boolean variable but don't quite understand how to format my algorithm.
#include <iostream>
using namespace std;
int main() {
bool first = true;
while (first) {
cout << "Enter a number (X to exit): ";
int num;
[Code]....
I know its jacked up any comments in the code would be great I need to finish the program but also understand it.
View 6 Replies
View Related
Oct 15, 2013
I want to store few different functions to a variable for different structs/classes and then call it later using that variable, is it possible? something like
struct item {
int ID;
int special; // for function
};
item Key;
Key.special = UseKey(KEY_KING);
// now when I want to call function "UseKey(KEY_KING)" I want to use "Key.special", like this
if(iCurrRoom == ROOM_KING)
Key.special;
else if(iCurrRoom == ROOM_DRAGON)
Fireball.special;
View 5 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
Sep 27, 2013
On my program I use a counter to count to 10, then i ask for a string, in this case "yes" or "no", during the count, i want to keep the user from putting inputs in, due to the fact that if they put both "yes" and "no" before the program reads the string.
Code:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include <string.h>
}
[code]....
View 3 Replies
View Related
May 1, 2014
# include <stdio.h>;
int main (void){
float judges[6];
int degree;
[Code].....
View 5 Replies
View Related
Jan 29, 2014
So I have a double array, where I'm inputting float numbers to certain points in an array. Sometimes, the numbers that are printed out are completely different from what I put in.Here is the part of the code:
Code: .
while( token != NULL ) {
num = atof(token);
test[j][i] = num;
printf( "
%s, i is %d, j is %d
", token,i,j );
printf( "number is %f
value test of i,j is %f
[code]....
Why the float num prints out fine, but when put into an array becomes garbage?I'm taking string values from a csv file and turning them into floats, but no problems seem to crop up there.I reset i when appropriate and increment j when needed, so I don't think my problems are from incorrect array values (though they might be)
View 2 Replies
View Related
Feb 8, 2015
I'm having a little problem where after reading a file I'm unable to update my struct with some particular info. It is actually a linkedlist. This is my struct:
Code:
struct node{
int id;
char name[50];
struct node *next;
}*head;
And in my Main function:
Code:
int main(){
int i, t_id, num;
struct node *temp;
char name[50], t_name[50], input_name[50], line[LINESIZE], *value;
struct node *head = NULL;
FILE * ifp = fopen("AssignmentOneInput.txt", "r");
[Code] .....
Basically in the while loop when I input something like this for updating numbers:
Code: temp->id = num; //These are numbers separated by a comma. or this:
Code: strcpy(temp->name,value); //These are names The program crashes
View 11 Replies
View Related
Apr 4, 2014
The first line of my input file is going to contain some number "T" which will represent the "combination length" of a list of random words. (In this case, they are Taco Bell items). The first number on the second line represents the number of unique items on the menu to get, and the second number on the second line represents the number of unique items that are supposed to be bought.
Basically the input will look like this: 2 3 2 taco burrito nacho
And the output looks like this: burritos nachos nacho taco burrito taco
This is what I have so far:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
int main(void){
int N, T, K;
char menu[N][20];
[Code] .....
What I am trying to get working right now is just to scan a file and put the strings into an array so then I can work on sorting the array. How can I add strings from a file into an array?
View 4 Replies
View Related
Aug 2, 2013
I have just learned about how one can put functions and headers in different files from the main() function. I was wondering if there is a rule of thumb of when and when not to separate the two, or if it's merely preference. Is there perhaps an "industry standard" regarding this, for those who program in C for a living?
View 4 Replies
View Related
Feb 14, 2015
I am at a loss with an assignment. I am supposed to read from a text file, with an input of something like this: alphaproleone,stroke,42 1 and Store it into an array of structures and then output it with each word/number starting on a new line. My current code prints out only the first part, and the "a" in alphaproleone is the actual number "21".
#include <iostream>
#include <string>
#include<fstream>
#include<iomanip>
using namespace std;
typedef struct drugtype {
string name, target;
int effectiveness, toxicity;
[Code]...
View 1 Replies
View Related
Apr 18, 2014
I am trying to put pieces of a csv file into an array, but i only want to put certain pieces of it. This is the information that i have
Player,Current Team, Age , Nat , Position ,From,To,Transfer fee
Gareth Bale, Real Madrid,24,Wales,RW,Tottenham,Real Madrid CF,91000000
Edinson Cavani,PSG,26,Uruguay,CF,SSC Napoli,PSG,64500000
Falcao, Monaco,27,Columbia,CF,Atlético Madrid,Monaco,60000000
Neymar, FC Barcelona,21,Brasil,LW,Santos,FC Barcelona,57100000
Mesut Özil, Arsenal,24,Germany,AM,Real Madrid ,Arsenal,50000000
James Rodríguez,Monaco, 21,Columbia,RW,FC Porto,Monaco,45000000
With this i want to skip the name and team but need age.
View 3 Replies
View Related
Oct 18, 2014
I am having trouble putting data from a txt file onto a vector of a class. For instance.
class employee{
setname(string) {
some code
} getname () {
some code
} setid(int)
[Code] ....
How would use my setname function in my class using my vector? I tried doing vec[num].setname(tempname) but it doesn't work. I know it does not work because I have to use .push back for a vector but I don't know exactly how to pushback using the set name function call.
View 10 Replies
View Related
Jul 7, 2014
I'm trying to pass several integers into the array idAr[10] from an input file using a for loop and then having them output on the console. The output should look like this:
#include <iostream>
#include <fstream>
using namespace std;
[Code].....
View 4 Replies
View Related
Dec 8, 2014
I am trying to use a combination of windows batch scripts as well a C++ program to put users data into one single .CSV file. Right now, I have a batch file that will output data from the command line into multiple .txt files. These files are mac.txt, serialnumber.txt, computermodel.txt, and computer name.txt. What I want to do, is to have users run the batch file, which will in turn run the .exe C++ file which will concatenate all their data into one file, computerinfo.csv. The file format for this file would be to have the mac address, serialnumber, computer model, and computer name all in their own column.
My main issue is that these individual files don't have the format that I would like, but based on the command prompt functions, there isn't really any good format. For example, the mac.txt file has the following format:
Physical Address Transport Name
=================== ==========================================================
xx-xx-xx-xx-xx-xx DeviceTcpip_{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
yy-yy-yy-yy-yy-yy Media disconnected
zz-zz-zz-zz-zz-zz Media disconnected
but all I really want is xx-xx-xx-xx-xx-xx
The other files have other issues with format, but if I can figure out this one then the others should be a piece of cake.
Also, I want the output of this to all go in one row, and as other users run this file, they will go into new rows without touching the rows above.
View 1 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