We have a homework problem that we had to modify. we had to make the user input their name and a radius to compute the area. The program was supposed to ask the name before it asked the radius. I have two problems i can not get the program to ask for a name first and i don't understand how to use the getline funtion for the users name.

Any help would be great.

Recommended Answers

All 10 Replies

Since your code is so small, you should post it between code tags , not to attach the file.
using namespace std; must be placed right after your include statement, and should not be included inside of a function.

#include <iostream>
#include <iomanip>
#include <string>

float circle_area(float &radius);
const float PI=3.14;


float circle_area(float &radius)
{
    using namespace std;
    string name;

    cout<< "Please enter name:";
    cin >> (name);

    float area= PI*radius*radius;
    cout<< "Hello, "<<name<<", the area of the circle is: "<< area << setprecision(2) << endl;
    return (area);
}

int main()
{
    using namespace std;


    float radius;

    cout<< "Enter radius of circle: ";
    cin >> radius;
    circle_area(radius);
    int user_input;
    cin >> user_input;
    return 0;
}

>using namespace std; must be placed right after your include statement
This isn't a requirement.

>and should not be included inside of a function.
Why not? It's perfectly legal and because using statements follow scoping rules, it has practical uses.

>float circle_area(float &radius);
Since your function is defined before main, there's no need for a prototype. Also, there's little reason to pass primitive types by reference unless you intend to change them within the function and have those changes reflected back in the calling function.

>const float PI=3.14;
Floating-point literals have type double. If you want to avoid the warning that you may get, postfix the literal with f or F.

I made the above changes to your code as well as moved the request for a name into main. I also cleaned up your area function and gave you a slightly better pause at the end of the program. Study the code carefully to see what changes I made and try to figure out why I made them.

#include <iostream>
#include <iomanip>
#include <string>

const float PI = 3.14f;

float circle_area(float radius)
{
  return PI * radius * radius;
}

int main()
{
  using namespace std;

  float radius;
  string name;

  cout<< "Enter radius of circle: ";
  cin>> radius;
  cout<< "Please enter name:";
  cin>> name;
  cout<< "Hello, "<<name<<", the area of the circle is: "
      << circle_area(radius) << setprecision(2) << endl;
  
  // Clear the stream
  while (cin.get() != '\n')
    ;
  // Pause
  cin.get();

  return 0;
}

>Since your function is defined before main, there's no need for a prototype
..unless other function defined before circle_area() calls circle_area() ;)

>Since your function is defined before main, there's no need for a prototype
..unless other function defined before circle_area() calls circle_area() ;)

Care to show me which function is defined before circle_area that calls it? Or are you just trying to act intelligent by bringing up hypothetical situations?

@Narue:
If I am wrong, please correct me. And if I am right, why do U insult me ?
No offense

>And if I am right, why do U insult me ?
You are right. Just as right as I would be to say "Unless the definition is moved so that it's after main". Just because you're right doesn't mean that your statement wasn't stupid.

The function is defined before main, therefore a prototype is not required. We could change things around any number of ways so that this statement is not true, but why waste time mentioning it when there's no obvious reason?

@Narue:
I respect your knowledge and the fact, that U try to help people. So am I. The sentence: "Since your function is defined before main, there's no need for a prototype" is not complete, so I wrote what I wrote. It wasn't for U, I know it's obvious for U. This was for the person who we both want to help understand. If U think it was stupid, I don't care. I hope U understand me. Greets.

>The sentence: <snip> is not complete
It's not complete because I didn't take into account every possible case. I was talking about the code given rather than all code because I wasn't inclined to write a "complete" tutorial about function declarations and definitions. Along the same line of thinking, your addition was also incomplete because you failed to consider other things such as moving the definition after main. You fell into the trap of trying to be completely accurate for the general case when it really wasn't necessary.

>I hope U understand me.
Well, I'm having a bit of trouble with your annoying habit of replacing "you" with "U". But since you don't care what I think, I'll just plonk you and be on my way.

*plonk*

>Well, I'm having a bit of trouble with your annoying habit of replacing "you" with "U"
I like your sarcasm, it's funny. But you are right, so I'll fix that U.

>*plonk*
Although sometimes you are irritating, I won't plonk you anyway.

Greets, and no word from me in this topic.

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.