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.

Recommended Answers

All 5 Replies

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;
}

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.