| View previous topic :: View next topic |
| Author |
Message |
Frostbyt3 Master Cheater
Reputation: 0
Joined: 07 Jan 2008 Posts: 323 Location: Australia
|
Posted: Fri Oct 24, 2008 11:55 pm Post subject: |
|
|
| Code: | // my second program in C++
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main ()
{
//Store input
string input;
//Is the program running
bool exit = false;
//Main loop
while (exit == false)
{
cout<<">";
//Get input and store it in input
getline (cin, input);
if (input == "quit")
{
exit = true;
}
}
} |
Enjoy. |
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Sat Oct 25, 2008 12:19 am Post subject: |
|
|
| Code: | int main(){
char input[50];
do{
std::cin >> input;
}while(strcmp(input, "quit") != 0);
return 0;
} |
|
|
| Back to top |
|
 |
xV I post too much
Reputation: 1
Joined: 03 Jan 2008 Posts: 3783 Location: Seattle
|
Posted: Sat Oct 25, 2008 2:13 am Post subject: |
|
|
| slovach wrote: | | Code: | int main(){
char input[50];
do{
std::cin >> input;
}while(strcmp(input, "quit") != 0);
return 0;
} |
|
D: I haven't even gotten that far yet. I can barely grasp variables. _________________
|
|
| Back to top |
|
 |
92Garfield I'm a spammer
Reputation: 57
Joined: 20 Dec 2007 Posts: 5871 Location: Banana Republic Germany
|
Posted: Sat Oct 25, 2008 2:50 am Post subject: |
|
|
| xViBeSz wrote: | | slovach wrote: | cin.sync
cin.ignore / get.
| Code: | #include <iostream>
int main (){
std::cout << "Hello World!";
std::cout << "I'm a C++ program";
std::cin.sync();
std::cin.ignore();
return 0;
} |
|
:O What's all that std stuff?
Could I get rid of all that std:: if I used: | Code: | | using namespace std; |
|
yes and for the prgramm not to close you could do
| Code: | cout<<"Press anything to exit or R to restart."<<endl;
cin>>cont; //must declare cont as a variable that may contain letters
if (cont == "R")
{
system("name of your prgram"); //batch code
} |
and you need to have this at top
you could also use
but it will close on any key then and u need
_________________
Last edited by 92Garfield on Sat Oct 25, 2008 2:52 am; edited 1 time in total |
|
| Back to top |
|
 |
TehBestNewbZ Expert Cheater
Reputation: 0
Joined: 28 Jul 2006 Posts: 109
|
Posted: Sat Oct 25, 2008 2:51 am Post subject: |
|
|
| Dude, learn from a book, not from here. |
|
| Back to top |
|
 |
92Garfield I'm a spammer
Reputation: 57
Joined: 20 Dec 2007 Posts: 5871 Location: Banana Republic Germany
|
Posted: Sat Oct 25, 2008 2:53 am Post subject: |
|
|
| TehBestNewbZ wrote: | | Dude, learn from a book, not from here. |
books aint good, they don't asnwer question and rephrase stuff you don't understand
the best is to ask some questions in forum and use google it answers questions _________________
|
|
| Back to top |
|
 |
Snootae Grandmaster Cheater
Reputation: 0
Joined: 16 Dec 2006 Posts: 969 Location: --->
|
Posted: Sat Oct 25, 2008 3:19 am Post subject: |
|
|
| Quote: | books aint good, they don't asnwer question and rephrase stuff you don't understand
the best is to ask some questions in forum and use google it answers questions |
books arent always good, but hes not asking question on-purpose, hes only doing it cos hes not following any tutorials, and no asking here is not a good idea at this early stage, go find some tuts and read them to learn the basics, then when you get better, ask us about things your unsure about, prefereably after a quick google _________________
|
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Sat Oct 25, 2008 3:39 am Post subject: |
|
|
| Code: | #include <iostream>
#include <string>
int main(int argc, char *argv[])
{
std::string input; // The input will be stored here.
do {
std::cout << "Type exit to exit: ";
std::getline( std::cin, input ); // Store the input from std::cin to input string
} while( input != "exit" ); // Compare the input to "exit"
return EXIT_SUCCESS;
} |
If you only want to pause your program at exit, use std::cin.sync() and std::cin.ignore(): | Code: | std::cout << "Press enter to exit.";
std::cin.sync();
std::cin.ignore(); |
I agree that reading books isn't always the best way.. Myself, I haven't read a single book about a programming language :) I'd prefer getting some assignments and then just start doing them and when in trouble, use Google or ask someone. The only good way to learn programming is really doing it, you don't learn how to program if you only read books.
slovach: There's an overflow in your program. So, don't do this like slovach does :P |
|
| Back to top |
|
 |
Stonehenge Master Cheater
Reputation: 0
Joined: 03 Oct 2008 Posts: 280 Location: Unknown Jumphole
|
Posted: Sat Oct 25, 2008 6:46 am Post subject: |
|
|
Here is a program I made to convert warp units into c units (speed of light)
| Code: | //
// Converts Warp speeds into c (light speed)
//
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <strings.h>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
double c;
c = 208.3333;
double warp;
cout << "Enter the warp speed you would like to convert into c units: ";
cin >> warp;
warp = warp * c;
cout << "The c speed conversion of the warp speed you entered is: ";
cout << warp;
cout << endl;
system("PAUSE");
return 0;
}
|
The "don't quit automatically" is in the
system("PAUSE")
return 0; _________________
|
|
| Back to top |
|
 |
dEEznutz Grandmaster Cheater
Reputation: 0
Joined: 29 Nov 2007 Posts: 514
|
Posted: Sat Oct 25, 2008 7:48 am Post subject: |
|
|
| Obidos wrote: | Here is a program I made to convert warp units into c units (speed of light)
| Code: | //
// Converts Warp speeds into c (light speed)
//
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <strings.h>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
double c;
c = 208.3333;
double warp;
cout << "Enter the warp speed you would like to convert into c units: ";
cin >> warp;
warp = warp * c;
cout << "The c speed conversion of the warp speed you entered is: ";
cout << warp;
cout << endl;
system("PAUSE");
return 0;
}
|
The "don't quit automatically" is in the
system("PAUSE")
return 0; |
Don't use system("pause"). cin.get() is better. _________________
Learning C++ |
|
| Back to top |
|
 |
92Garfield I'm a spammer
Reputation: 57
Joined: 20 Dec 2007 Posts: 5871 Location: Banana Republic Germany
|
Posted: Sat Oct 25, 2008 9:16 am Post subject: |
|
|
system is to use batch code right? so it would work only with windows _________________
|
|
| Back to top |
|
 |
Noz3001 I'm a spammer
Reputation: 26
Joined: 29 May 2006 Posts: 6220 Location: /dev/null
|
Posted: Sat Oct 25, 2008 9:33 am Post subject: |
|
|
| 92Garfield wrote: | | system is to use batch code right? so it would work only with windows |
It works with all OS |
|
| Back to top |
|
 |
92Garfield I'm a spammer
Reputation: 57
Joined: 20 Dec 2007 Posts: 5871 Location: Banana Republic Germany
|
Posted: Sat Oct 25, 2008 10:41 am Post subject: |
|
|
| noz3001 wrote: | | 92Garfield wrote: | | system is to use batch code right? so it would work only with windows |
It works with all OS |
im gonna try so its not batch code? _________________
|
|
| Back to top |
|
 |
xV I post too much
Reputation: 1
Joined: 03 Jan 2008 Posts: 3783 Location: Seattle
|
Posted: Sat Oct 25, 2008 11:29 am Post subject: |
|
|
| Snootae wrote: | | Quote: | books aint good, they don't asnwer question and rephrase stuff you don't understand
the best is to ask some questions in forum and use google it answers questions |
books arent always good, but hes not asking question on-purpose, hes only doing it cos hes not following any tutorials, and no asking here is not a good idea at this early stage, go find some tuts and read them to learn the basics, then when you get better, ask us about things your unsure about, prefereably after a quick google |
Okay, this is the very first thing that I started to read (the first tutorial) and I used their example code and read the descriptions of what the codes do, but the problem was it was exiting so quick I couldn't even read it and the tutorial didn't tell me to add anything to stop that. >.<
And someone told me to use
| Code: | cin.sync();
cin.ignore(); |
And that works really good and it's simple. Thanks. _________________
|
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Sat Oct 25, 2008 6:47 pm Post subject: |
|
|
| 92Garfield wrote: | | system is to use batch code right? so it would work only with windows | ::system() should work on every OS, but ::system("pause") is Windows specific: | Code: | RH:(~)(52)% pause
pause: Command not found. |
|
|
| Back to top |
|
 |
|