#Help with main function not exiting when another function has return 0;

30 messages · Page 1 of 1 (latest)

prisma grove
#

I made the 'choices' function so I wouldn't have to include that switch case inside my main function, but whenever the default case happens, my main function doesn't exit and it keeps running.

desert cypressBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

prisma grove
#

am I doing something wrong?

dusky elm
#

You need to check the return value when you call the Choices function

#

Make sure to return a value from all paths in your Choices function as well

#

Also please don’t tell me you did

#define ZERO 0
#define ONE 1
#define FORTYFIVETHOUSAND 45000```
prisma grove
#

Yeah. had to cause that's what my prof wants

dusky elm
#

Why??

prisma grove
#

lol idk. but how do I check the return when I call it?

#

kinda new to all this

spare nacelle
#

You can

  • A) call exit from your choices function or
  • B) store the return value of choices and proceed based on that

Btw: Your choices function as of right now isn't complete because it only returns something in the default case, but nothing in any other case. Are you compiling with -Wall -Wextra?

#

Also:

#

!sc

desert cypressBOT
# spare nacelle !sc
Monke
Please Do Not Send Screenshots!

They're hard to read and prevent copying and pasting.

spare nacelle
#

!code

desert cypressBOT
#
How to Format Code on Discord
Markup

```c
int main() {}
```

Result
int main() {}
prisma grove
#

Could I just send the c file instead?

spare nacelle
#

yup

prisma grove
prisma grove
#

ignore if this isn't the most optimal way to do things still learning

spare nacelle
# prisma grove the cases do return the print functions but I'm not able to exit when using the ...

They don't "return the print functions".
That doesn't make sense as a sentence.
The print functions just produce a side effect of printing text to the screen. After the print your code runs into the break which jumps past the switch-case but there is no return ... anywhere to be seen (other than the default case).

Are you compiling with warnings enabled? If not then change that. As I said the warning flags are -Wall and -Wextra. Also compile with -Werror so that warnings are being treated as errors and your code doesn't even compile in these cases.

Next issue is that you're not actually storing the return value of the Choices function, but just calling it like you would call any void function like printbinary or BitCheck when it in fact should return an integer that you should store and based on that return value you can then determine whether the function was successful or not (e.g. a return value of 0 would mean unsuccessful while a return value of 1 would mean successful but that's up for you to choose).

prisma grove
#

I wasn't compiling with -Wall. didn't even know that was a thing. I'll be sure to do that and I'm understanding what you're saying now about the return

#

my bad about the "return print function" 😭 thought you meant like if it does anything. I'll work on this

spare nacelle
#

I'll work on this
Just started Rainbox Six Siege

#

🐒

prisma grove
#

I'll work on this after a short break 😔 been doing it since the morning

spare nacelle
#

all good, you do you.
I don't care when or if you do it. That's your bread and butter.

Just thought it was funny

prisma grove
#

cat_up Thanks for the help tho. Learned something new about compiling

desert cypressBOT
#

@prisma grove Has your question been resolved? If so, type !solved :)