944,051 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2000
  • C++ RSS
Jan 7th, 2005
0

help

Expand Post »
Hi i am learning C++
i typed the following programm
it is asking me to enter the age , when i enter age and hit eneter button it is generating the following message
test2.exe has generated errore and will be closed by windows you will need to resterat the programm
an error log is being created
i did't underatsnd whats wrong with the programm
i think it some thing relates to linking the programm
can any one help me in this matter

i am doing this in windows 2000 using dev C++ compiler

#include <iostream.h>
#include <stdlib.h>


void main()
{
int age;
cout<<"enter age\n";
cin>>"age";
if(age<=18)
cout<<"child\n";
cin.get();
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gtsreddy is offline Offline
7 posts
since Jan 2005
Jan 7th, 2005
0

Re: help

Quote originally posted by gtsreddy ...
Hi i am learning C++
i typed the following programm
it is asking me to enter the age , when i enter age and hit eneter button it is generating the following message
test2.exe has generated errore and will be closed by windows you will need to resterat the programm
an error log is being created
i did't underatsnd whats wrong with the programm
i think it some thing relates to linking the programm
can any one help me in this matter

i am doing this in windows 2000 using dev C++ compiler

#include <iostream.h>
#include <stdlib.h>


void main()
{
int age;
cout<<"enter age\n";
cin>>"age";
if(age<=18)
cout<<"child\n";
cin.get();
}

that should work.... although i would declare it has

int main()
{
int age = 0;

cout << "Please enter your age" << endl; //(endl creates new line)
cin >> age;

if ( age <=18)
cout << "Child";
else
cout << "Adult";

return 0;
}
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004
Jan 7th, 2005
0

Re: help

it is compiling well ,
asking for age
when i enter age and press enter button
i am not seeing the result on the window , it is disapperaing
even if i use cin.get()
it is not displaying the result
is there any thing to do with linking out put file ????






Quote originally posted by Acidburn ...
that should work.... although i would declare it has

int main()
{
int age = 0;

cout << "Please enter your age" << endl; //(endl creates new line)
cin >> age;

if ( age <=18)
cout << "Child";
else
cout << "Adult";

return 0;
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gtsreddy is offline Offline
7 posts
since Jan 2005
Jan 7th, 2005
0

Re: help

you could try the system pause? ...thats all i can think of for the moment but i'm sure the others on this forum wil think of something better to use

system("pause")

i think
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004
Jan 7th, 2005
0

Re: help

add
C++ Syntax (Toggle Plain Text)
  1. std::string dummy;
  2. cin << dummy;

your cin.get() was probably reading the enter you pressed...
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Jan 7th, 2005
0

Re: help

when i use
system pause command
it is displaying the result on the output window
thanks

Quote originally posted by jwenting ...
add
C++ Syntax (Toggle Plain Text)
  1. std::string dummy;
  2. cin << dummy;

your cin.get() was probably reading the enter you pressed...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gtsreddy is offline Offline
7 posts
since Jan 2005
Jan 7th, 2005
0

Re: help

Another trick is to simply trap the enter key with another cin.get() like this:
[php]
#include <iostream>

using namespace std;

int main()
{
int age;

cout << "enter age\n";
cin >> age;
if (age <= 18)
cout << "child\n";

cin.get(); // trap enter
cin.get(); // wait
return 0;
}
[/php]
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Jan 7th, 2005
0

Re: help

Hello,

I believe that there is a flag on the icon from within Windoze that controls window behavior. Look for something along the lines of closing window when done executing. Look in the Program tag, and uncheck "Close on exit".

Christian
Team Colleague
Reputation Points: 121
Solved Threads: 57
Posting Virtuoso
kc0arf is offline Offline
1,629 posts
since Mar 2004
Jan 8th, 2005
0

Re: help

Quote ...
#include <iostream.h>
#include <stdlib.h>


void main()
{
int age;
cout<<"enter age\n";
cin>>"age";
if(age<=18)
cout<<"child\n";
cin.get();
}
just in case you missed it. age should NOT have "" around it as it is a variable that you are intending to store the age in. This would cause the error (cin acesses bad memory i think ) Acidburn and vegaseat corrected it without realising! * claps * They are also correct about the get function. Your last cout put a '\n' character at the end so the next cin.get will read that '\n' and pass on. Another cin.get or system("PAUSE") or even a getchar() (needs #include <stdio>) gets the job done.
Reputation Points: 16
Solved Threads: 6
Posting Pro in Training
1o0oBhP is offline Offline
445 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: queue , memory, 32k please
Next Thread in C++ Forum Timeline: an "(" expected??





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC