How to return the response from an asynchronous function?

Simple C++ Function help?

  • I just started Sam's Teach Yourself C++ in an Hour a Day (21 days) and I haven't been having problems yet but there is one function that is stumping me. I have tried to comprehend the instructions given yet my brain has turned to mush and I can't figure it out. Here is the code: #include <iostream> int Add (int first, int second) { std::cout << "Add() recieved"<< first << " and "<< second << "\n"; return (first + second); } int main() { using std::cout; using std::cin; cout << "I'm in main()!\n"; int a, b, c; cout << "Enter two numbers: "; cin >> a; cin >> b; cout << "\nCalling Add()\n"; c=Add(a,b); cout << "\nBack in main().\n"; cout << "c was set to " << c; cout << "\nExiting...\n\n"; char response; std::cin >> response; return 0; } Where is says "Enter two numbers," the instructions say: The program prompts the user for two numbers . The user types each number, separated by a space, and then presses the Enter key. The numbers the user enters are placed in the variables a and b on lines 17 and 18. Every time I try to accomplish this, I build a solution and there are errors all over the place. How would I go about submitting the two numbers into the code and not getting those errors?

  • Answer:

    Works fine for me.

Toni at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

The code looks fine. What do the errors say?

I copied your code exactly, and it works perfectly fine for me. Nary a single error. I'm using GCC version 4.1.2 on Linux, but I doubt if that's an issue. The only thing weird is where you have it print "Exiting" then sit and wait for yet one more input before actually exiting. Otherwise, it works pretty much the way I expect it to. Maybe you should report the exact text of the first couple of errors you get, and that might help others diagnose your problems. EDIT as per Additional Details: I would guess that, since your program is written to request a third input after entering the first two numbers, that possibly your output is buffered. If this is the case, the output won't happen until either the buffers fill up, the output is flushed, or the program ends. What happens if you run it, give it your two numbers, then press the "enter" key one more time? Or, if you give it one more number followed by "enter" ??

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.