LiveWire Network Peer Answers Peer Support Teen Forums Tech Forums College Forums 531 users online 211072 members 886 active today Advertise Here Sign In
TeenCollegeTechPhotos | Quizzes | LiveSecret | Memberlist | Dictionary | News | FAQ
Member Spotlight
Chasey
Peeves: Gay people
Mood: Frustrated
You have 1 new message.
Emergency Help
Until you sign up you can't do much. Yes, it's free.

Sign Up Now
Membername:
Password:
Already have an account?
Invite Friends
Active Members
Groups
Contests
Moderators
6 online / 25 MPM
Fresh Topics
  LiveWire / Technical Forums / Programming & Application Development / Viewing Topic

HALP with C++.
Replies: 6Last Post Dec. 7, 2007 9:27am by Rhapsody
Welcome to LiveWire!
We're Stronger Together.
Join the Community
Single page for this topic Email Print Favorite
( RossTheHoss69 )


Enlightened One

Patron
Tech Support Leader
Reply
I need help with this C++ program.
NOTE: I am a beginner, so it's rather primitive.
Code:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
using namespace std;

int main()
{
   int Ag;
   string NameA;
   string NameB;
   cout << "Hello!\nWelcome To Josh's World!\n";
   system("PAUSE");
   cout << "First, though, I'll need some Info:\n";
   system("PAUSE");
   cout << "Your First Name:"<<endl;
   cin >> NameA;
   cout << "Your last Name:"<<endl;
   cin >> NameB;
   system("PAUSE");
   cout << "And I must confirm that you are over 13 years old.\nYour Age?";
   cin >> Ag;
   int age();
   cout <<"Hello!";
   system("PAUSE");
   return 0;
}
int age()
{
          int Ag;
          if (Ag < 13);
                        {
                         cout << "You are TOO young!";
                         system("PAUSE");
                         return 0;
                        }
          else        
                        {
                         cout << "Welcome To Josh's World!";
                         system("PAUSE");
                         return main();
                        }
}


It keeps telling me there is an error on the
Code:
else
line.
Pissing me off.

Any help is appreciated.

-------
I'm not changing this line until I get a girlfriend. [7-08-07; 1:26 a.m.]
Click for n00dz.
Shït. Piss. Fück. Cünt. Cocksucker. Motherfücker. And Tits.
Rest In Peace, George Carlin.


5:28 pm on Nov. 6, 2007 | Joined: Dec. 2005 | Days Active: 887
Join to learn more about RossTheHoss69 Texas, United States | Male | Posts: 5,421 | Points: 14,988
Bzoink


♠ ♣ ♥ ♦

Patron
Reply
I thought ifs didn't have semicolons after them...

5:29 pm on Nov. 6, 2007 | Joined: Nov. 2005 | Days Active: 444
Join to learn more about Bzoink Oregon, United States | Label Free Male | Posts: 14,130 | Points: 40,536
Zaneoc


Professional
Reply
Probably the fact that it pauses, PM me I can help you with C++ also.

-------
I will do any photoshop job for free. I have plenty of free time.
"I'll take you for who you are, if you take me for everything."

5:29 pm on Nov. 6, 2007 | Joined: Oct. 2007 | Days Active: 155
Join to learn more about Zaneoc Iowa, United States | Straight Male | Posts: 824 | Points: 2,376
Zaneoc


Professional
Reply
Quote: from Bzoink at 5:29 pm on Nov. 6, 2007

I thought ifs didn't have semicolons after them...

Oh damn, I didn't notice that, that's definitely it, get rid of the semicolon after If (condition)

-------
I will do any photoshop job for free. I have plenty of free time.
"I'll take you for who you are, if you take me for everything."


5:31 pm on Nov. 6, 2007 | Joined: Oct. 2007 | Days Active: 155
Join to learn more about Zaneoc Iowa, United States | Straight Male | Posts: 824 | Points: 2,376
Bzoink


♠ ♣ ♥ ♦

Patron
Reply
Quote: from Zaneoc at 8:31 pm on Nov. 6, 2007

Quote: from Bzoink at 5:29 pm on Nov. 6, 2007

I thought ifs didn't have semicolons after them...

Oh damn, I didn't notice that, that's definitely it, get rid of the semicolon after If (condition)


Mmm thought so.

5:32 pm on Nov. 6, 2007 | Joined: Nov. 2005 | Days Active: 444
Join to learn more about Bzoink Oregon, United States | Label Free Male | Posts: 14,130 | Points: 40,536
sakurag


Soothsayer

Patron
Reply
Careful with 'return main();'



-------
I've got spurs that jingle jangle jingle.
As I go riding merrily along.
And they sing, "Oh, ain't you glad you're single?"
And that song ain't so very far from wrong.


1:59 pm on Nov. 7, 2007 | Joined: Oct. 2002 | Days Active: 748
Join to learn more about sakurag Washington, United States | Straight Male | Posts: 2,548 | Points: 11,250
Rhapsody


Per Ardua Ad Astra

Patron
Tech Support Leader
Reply
Okay I have been able to resolve the program and make it work as I thought it should. I hope what I did helps you. Please compare the two codes against each other to see what was changed.  

From reading the code, I can see that as it stands "age" is meant to be a function of the Main program. When declaring a function you should always place the function above the main function. As when compiling it will read from the top and come down, however if you do not have the function before the main, you will get a lot of errors. At sometimes because of this it gives you a lot of things as errors which really aren't, like for example that parse error after the else.  

Taking into account that the age does not really return any kind of value, it should be made of data type void as opposed to int. This is where I believe more errors occurred.  

Please read these notes on Functions to more understand what I have been talking about.

Here is the corrected code:
Code:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
using namespace std;

void age(void)
{
         int Ag;
         cout << "And I must confirm that you are over 13 years old.\nYour Age?";
         cin >> Ag;
         if (Ag <= 12)
                       {
                        cout << "You are TOO young!";
                        system("PAUSE");
                        return ;
                       }
         else
                       {
                        cout << "Welcome To Josh's World!";
                        system("PAUSE");
                        return ;
                       }
}  

int main()
{
  int Ag, ans;
  string NameA;
  string NameB;
  cout << "Hello!\nWelcome To Josh's World!\n";
 // system("PAUSE");
  cout << "First, though, I'll need some Info:\n";
//  system("PAUSE");
  cout << "Your First Name:"<<endl;
  cin >> NameA;
  cout << "Your last Name:"<<endl;
  cin >> NameB;
 // system("PAUSE");
  age();
  cout <<"Hello!";
  system("PAUSE");
  return 0;
}

Note: I commented out some of those system pauses, they were getting annoying to see while running the program.  

EDIT: I almost forgot, I also moved the prompt and read age lines to the function Age, because while they were in the main function, when control was handed over to the age function the number stored in Ag was not carried over to the age function, there for the if statement was not being used properly. In that no matter what the age was, they would still get the "Welcome to Josh's World" message as opposed to the other one.

Post edited at 12:53 pm on Dec. 7, 2007 by Rhapsody

-------
The greatest feeling in the world is love, but the worse feeling
is to love someone and not be loved back. Stop breaking my heart
There is only one guarantee in life, and that is NOW.


9:27 am on Dec. 7, 2007 | Joined: Oct. 2005 | Days Active: 1,021
Join to learn more about Rhapsody Barbados | Male | Posts: 12,801 | Points: 30,804
Single page for this topic Email Print Favorite

Quick Reply

You are signed in as our guest.

Looking for something else?
 

  LiveWire / Technical Forums / Programming & Application Development / Viewing Topic