Ok the thing I searched into google brought up a ton of nonsense. I want to use variables from a function in main and (more important) variables from main in a function. I am already using file I/o as a loophole but I want an easier way.
PS How do I combine strings? Again my loophole is file io but it takes so long to type. The one I found on google gave me a buch of 'error strut instant to char crap as I am using this to get a filename has to be in c style or else the compiler gives me grief.

Edited 5 Years Ago by Zssffssz: n/a

Ok the thing I searched into google brought up a ton of nonsense. I want to use variables from a function in main and (more important) variables from main in a function.

Declare the variables globally.i.e before main()

#include<iostream>
using namespace std;
//declare variables here

int main(){}

Ok the thing I searched into google brought up a ton of nonsense.

Lack of understnding does not make the information nonsense...

I want to use variables from a function in main...

Pass the values back to main() when the function returns.

... and (more important) variables from main in a function.

Pass the values into the function in the parameter list.

Both of these can easily be found in any tutorial or book on C/C++.

PS How do I combine strings?

Depends on the type of 'string' you use. Either a simple + if you're using strings or strcat() if using char*

Declare the variables globally.i.e before main()

Yikes...Is that good advice?

Try reading up on references or pointers. I would try references first before plunging into pointers.

Simple reference example.

#include <iostream>

void square_it(int & i)//pass by reference
{
  i *= i;
}

int main()
{
  int i = 5;
  
  std::cout << i << std::endl;
  
  square_it(i);
  
  std::cout << i << std::endl;
  
  return 0;
}

Edited 5 Years Ago by gerard4143: n/a

Whats so bad about global vareable? Also by nonsence I mean stuff not revalent to what I am asking. Now to my other respnse.
!?!?! How did I not know about global vareables!?!? What else can I do up there.

This question has already been answered. Start a new discussion instead.