•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 401,653 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,645 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Views: 1508 | Replies: 6
![]() |
•
•
Join Date: Dec 2004
Location: England
Posts: 10
Reputation:
Rep Power: 4
Solved Threads: 0
Hey! I GUARANTEE that I'm the most junior programmer here! I started programming VB about 2 months ago, and I started Dev-C++ yesturday!!! I actually have a quick question regarding Dev-C++, I would be very grateful for an answer on this ^_^
I have just purchaced the "Beginning C++ Game Programming" by Michael Dawson... and I'm very confused about the following function:
std::cout << "Press the enter key to exit";
std::cin.ignore(std::cin.rdbuf()->in_avail() + 1);
I am aware that by "int"ing the cout and cin things, I no longer have to type in the std bit:
cout << "Press the enter key to exit";
cin.ignore(cin.rdbuf()->in_avail() + 1);
But it just WONT work for some reason! As soon as the program has compiled, the screen flashes on and flashes off! Do you know how to make this function available to all of my created programs, so that I am able to manually close down my created program without the fustration of automatic return 0;!
For example, (I don't know if anyone is familiar with this source code, but I'll give it a mention anyway):
The "Lost_Fortune" program asks the user to input a number, then a smaller number and then your surname... I enter all of these things and then, (when it is supposed to show me my results) it flashes on and off so fast that I can't read it! I'm sure that you have had millions of junior programmers such as myself asking the same old question, but I would be grateful for a reply! Thanks all!
Thank you VERY much!!! All the best.
I have just purchaced the "Beginning C++ Game Programming" by Michael Dawson... and I'm very confused about the following function:
std::cout << "Press the enter key to exit";
std::cin.ignore(std::cin.rdbuf()->in_avail() + 1);
I am aware that by "int"ing the cout and cin things, I no longer have to type in the std bit:
cout << "Press the enter key to exit";
cin.ignore(cin.rdbuf()->in_avail() + 1);
But it just WONT work for some reason! As soon as the program has compiled, the screen flashes on and flashes off! Do you know how to make this function available to all of my created programs, so that I am able to manually close down my created program without the fustration of automatic return 0;!
For example, (I don't know if anyone is familiar with this source code, but I'll give it a mention anyway):
The "Lost_Fortune" program asks the user to input a number, then a smaller number and then your surname... I enter all of these things and then, (when it is supposed to show me my results) it flashes on and off so fast that I can't read it! I'm sure that you have had millions of junior programmers such as myself asking the same old question, but I would be grateful for a reply! Thanks all!
Thank you VERY much!!! All the best.
•
•
Join Date: Apr 2004
Posts: 555
Reputation:
Rep Power: 6
Solved Threads: 6
You need the std:: in front of cout and cin. You are using the standard namespace. You use it when you are using the <iostream> header file because it is wrapped in the standard namespace. (I could be wrong. I am going to check it out and correct it if I have too). Can you post the whole code, this might be helpful. Put the code in code tags.
The program that you had trouble seeing is in dos I'm assuming. To fix this open up the dos command prompt. Then you have to find the directory you put it in and tell dos to open it, or you can simply drag the program into the dos window and press enter. This makes the window stay open so you can see the program.
It also might be good to get familar with c++ before you start trying to game program. It will help you out in the long run, and will make everything clearer.
The program that you had trouble seeing is in dos I'm assuming. To fix this open up the dos command prompt. Then you have to find the directory you put it in and tell dos to open it, or you can simply drag the program into the dos window and press enter. This makes the window stay open so you can see the program.
It also might be good to get familar with c++ before you start trying to game program. It will help you out in the long run, and will make everything clearer.
•
•
Join Date: Dec 2004
Location: England
Posts: 10
Reputation:
Rep Power: 4
Solved Threads: 0
Ok, I didn't know how do put this snippet into the C++ section, so here it is...
Sorry if you wanted to read it in the code section!
Now obviously I would put in the:
std::cout << "Press the enter key to exit";
std::cin.ignore(std::cin.rdbuf()->in_avail() + 1);
bit right before the return 0; but even when I try that it won't stay open for more than a split second... I hope this is going to help you understand my problem. You'll probably find that DOS opens and closes REALLY fast for you too, but i'm sure you'll be able to figure something out!
Thanks DarkOmen!
Sorry if you wanted to read it in the code section!
#include <iostream>
#include <string>
using std::cout;
using std::cin;
using std::endl;
using std::string;
int main()
{
const int GOLD_PIECES = 900;
int adventurers, killed, survivors;
string leader;
//get the information from the user:
cout << "Welcome to Lost Fortune\n\n";
cout << "Please enter the following for your personalized adventure\n";
cout << "Enter a number: ";
cin >> adventurers;
cout << "Enter a number, smaller than the first: ";
cin >> killed;
survivors = adventurers - killed;
cout << "Enter your last name: ";
cin >> leader;
//tell the story:
cout << "\nA brave group of " << adventurers << " set out on a quest ";
cout << "-- in search of the lost treasure of the Ancient Dwarves. ";
cout << "The group was led by that legendary rogue, " << leader << ".\n";
cout << "\nAlong the way, a band of marauding ogres ambushed the party. ";
cout << "All fought bravely under the command of " << leader;
cout << ", and the ogres were defeated, but at a cost. ";
cout << "Of the adventurers, " << killed << " were vanquished, ";
cout << "leaving just " << survivors << " in the group.\n";
cout << "\nThe party was about to give up all hope. ";
cout << "But while laying the deceased to rest, ";
cout << "they stumbled upon the buried fortune. ";
cout << "So the adventurers split " << GOLD_PIECES << " gold pieces.";
cout << leader << " held on to the extra " << (GOLD_PIECES % survivors);
cout << " pieces to keep things fair of course.\n";
return 0;
}Now obviously I would put in the:
std::cout << "Press the enter key to exit";
std::cin.ignore(std::cin.rdbuf()->in_avail() + 1);
bit right before the return 0; but even when I try that it won't stay open for more than a split second... I hope this is going to help you understand my problem. You'll probably find that DOS opens and closes REALLY fast for you too, but i'm sure you'll be able to figure something out!
Thanks DarkOmen!
Last edited by alc6379 : Dec 16th, 2004 at 5:21 pm. Reason: added [code] tags
its crude but if you add the following near the top (before main)
then use the function getchar like so:
IE:
#include <cstdio> using namespace std; // you dont need std:: if you use the std names (this means you could get rid of the "using... " statements...
then use the function getchar like so:
cout << "Press the enter key to exit"; getchar(); // replaces the cin.ignore stuff return 0;
IE:
#include <iostream>
#include <string>
#include <cstdio>
using namespace std;
int main()
{
const int GOLD_PIECES = 900;
int adventurers, killed, survivors;
string leader;
//get the information from the user:
cout << "Welcome to Lost Fortune\n\n";
cout << "Please enter the following for your personalized adventure\n";
cout << "Enter a number: ";
cin >> adventurers;
cout << "Enter a number, smaller than the first: ";
cin >> killed;
survivors = adventurers - killed;
cout << "Enter your last name: ";
cin >> leader;
//tell the story:
cout << "\nA brave group of " << adventurers << " set out on a quest ";
cout << "-- in search of the lost treasure of the Ancient Dwarves. ";
cout << "The group was led by that legendary rogue, " << leader << ".\n";
cout << "\nAlong the way, a band of marauding ogres ambushed the party. ";
cout << "All fought bravely under the command of " << leader;
cout << ", and the ogres were defeated, but at a cost. ";
cout << "Of the adventurers, " << killed << " were vanquished, ";
cout << "leaving just " << survivors << " in the group.\n";
cout << "\nThe party was about to give up all hope. ";
cout << "But while laying the deceased to rest, ";
cout << "they stumbled upon the buried fortune. ";
cout << "So the adventurers split " << GOLD_PIECES << " gold pieces.";
cout << leader << " held on to the extra " << (GOLD_PIECES % survivors);
cout << " pieces to keep things fair of course.\n";
cout << "Press enter to exit";
getchar();
return 0;
} http://sales.carina-e.com
no www
no nonsense
coming soon to a pc near you! :cool:
no www
no nonsense
coming soon to a pc near you! :cool:
•
•
Join Date: Feb 2002
Location: Lawn Guylen, NY
Posts: 10,888
Reputation:
Rep Power: 32
Solved Threads: 110
•
•
•
•
Originally Posted by xinix
Ok, I didn't know how do put this snippet into the C++ section, so here it is...
Sorry if you wanted to read it in the code section!
The code snippet library is a resource of fully working code snippets and functions. So you did right posting it here
Dani the Computer Science Gal
Do you run a computer-related website? Feature it in our niche link directory!
Do you run a computer-related website? Feature it in our niche link directory!
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
api apple asm assembly x86 programming hla demo blogger blogging c c++ ccna cocoa code coding competition compiler compilers computer debugging developer development errors evaluation framework gdata google high-performance innovation java languages linerider mcse microsystems networking news next object online open oriented planning platform practices programming python rss software step steps sun tools tutorials xml
- Hours a day on the comp (Geeks' Lounge)
- Bell (Visual Basic 4 / 5 / 6)
- which language ? (Computer Science and Software Design)
- Networking old ThinkPad (Networking Hardware Configuration)
- How to connect 2 remote comps (Networking Hardware Configuration)
Other Threads in the C++ Forum
- Previous Thread: Name & address code- help!
- Next Thread: How to access a variable of a diff class



Linear Mode