Functions

I have beginning c++ course at school. I just started learning about functions. I have a homework assignment and I'm conceptually confused about what to do. I have to create a couple of different functions for printing addresses.
For example;
I will have to read from a input file following this type of form. The first line is the print selection so,
3
John Smith
123 Main Street
Anywhere, MI. 99999-0000
Here is the kicker blank lines can occur between anything so from the example above maybe a blank line between the print selection and the name.
So I thought that I would start by writing just one function to do a specific job.
Example
PrintAddress()-prints out all three lines of an address with package formatting.
Question;If a function takes two parameters say like double x and double y. How on earth would I input something like a the name John smith and give back an address. Like I said we only had one lecture covering this.
Any conceptual clarity would be nice. Hopefully this makes sense.
Read address from file. Output address. Start by writing that code. Then worry about functions.
ok thx
OK I have began trying to write my functions. I have a problem.
I wrote one function to return an integer it works. I wrote another to return 3 lines , it works. My question is when I try to do both of them in main it does not work. If I comment one and run it it will work. If I comment the other and uncomment the first and run , it will work. Basically both functions work but they do not work together. I cannot run my program and the integer function displays then the other function for the strings. Does anyone have any ideas on what I might be doing wrong?
closed account (3qX21hU5)
could you paste the code of what you have so far? Would help us know more about it.
#include<iomanip>
#include<iostream>
#include <string>
#include <fstream>
using namespace std;
// Global constants

fstream fin("input.txt");

//Purpose: To create a PrintAdress() funtion which prints out the label with no formatting.
int PrintSelection()
{
int choice=0;
int nullchoice=-1;
fin.open("inputpractice.txt");

{
fin>>choice;

if((choice>0)&&(choice<=3))

return choice;

else if((choice<0)&&(choice>3))
choice= nullchoice;

return nullchoice;

fin.close();

}


}


void GetAddress(string& str1, string& str2, string& str3)
{
fin.open("inputpractice.txt");

fin.ignore(1,'\n');

getline(fin,str1);

getline(fin,str2);

getline (fin,str3);






}

void main()
{

//Variables
string str1="";
string str2="";
string str3="";



//Function testing

cout<<PrintSelection();

GetAddress(str1,str2,str3);
cout<<str1;



system("pause");


}



It's changed a little bit now because I was messing with it since the last post but it's basically the same. You can get the idea all I'm doing is writing functions and trying to see if they work in main before I put everything together.



NEVERMIND! I GOT IT. AT LEAST THIS PART HA THX
Last edited on
void main()

That's not C++. In C++, main returns an int. Always and forever. If your compiler doesn't even warn you about this, worry about what else it's not telling you.
int main()
Yes this is C++ main is a void function so it returns not a thing.
We always use main in the manner in my course.
I gather it is possible to get away with:

1
2
3
main() {

}


but not:

1
2
3
void main() {

}


As Moschops says. I would be very inclined to listen to Moschops if I was you :=D

Apparently, in more recent compilers, main implicitly returns an int, even if you don't explicitly return something.

Having said that I have always done this since 1987:

1
2
3
4
int main () {

return 0; // if all is well, something else otherwise
}




Last edited on
Yes this is C++ main is a void function so it returns not a thing.

No, it is not. main returns int. The C++ definition is very, very clear. main returns int. The big, official document that explains what C++ actually is has it written very, very clearly. If your main does not return int, it's wrong. If your compiler doesn't complain, it is wrong as well (interestingly, I am told that MS Visual C++ doesn't complain - I'm not surprised - MS never did like having to conform to standards, including its own standards).

We always use main in the manner in my course.

Your teacher is teaching you incorrect C++.

What you're doing in your course is not correct C++. I accept that you don't have a choice, but at least now you know, so that if you ever have to use correct C++ you won't make this mistake.

I gather it is possible to get away with:
1
2
3
main() {

}

I think this is an old, old hangover from C. Back in the day, C would assume an int return on a function that wasn't specified.
Last edited on
I have always done it how I have shown it, it was soon after I joined this forum, that someone pointed out about the implicit return of an int.

Any way, I don't wish to argue - it's all a bit academic & I think we are saying the same thing.

Cheers
Last edited on
I ran into the same issue (I'm also taking the beginning C++ course). Visual Studio compiles void main() but I get an error doing that in Xcode. Using int main(){return 0;} works. I searched this and everything I found says int main() is the correct way, my teacher told me to use whatever works for me.
haha ok thanks
that someone pointed out about the implicit return of an int.


0 will be implicitly returned from main if you don't supply your own return statement+value, however there is no implicit return type. int must be explicitly supplied in main's definition.
Topic archived. No new replies allowed.