![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Jul 2005
Posts: 66
Rep Power: 4
![]() |
Simple question! Strings!
Hello, I'm making a text based game with C++, and my book never covered strings, (not where I am) and I am trying to do a simple program that gets a name from a user, and it prints it back out:
#include <iostream>
int main(){
while (1 == 1) {
string name;
cout << "WELCOME TO MAGE WARS v1.0! \n" ;
cout << "Hello there! What's your name?: \n";
getline (cin, name);
cout << "Welcome,";
cout string;
cout << ", to Mage Wars! We are in a world of need, and we are hoping a true";
cout << "hero like you would help us! Please! Destroy the evil mages!";Thanks! Arnack |
|
|
|
|
|
#2 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4
![]() |
replace
cout << string; with cout << name; Name refers to the variable name, "string" refers to the type of the variable.
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Jul 2005
Posts: 66
Rep Power: 4
![]() |
Nevermind, I got it working. Thanks so much man! I might be putting some more questions up in this thread.. hopefully that isn't a hassle for you all!
-Arnack |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Jul 2005
Posts: 66
Rep Power: 4
![]() |
Okay, can anyone tell me how to print an int that creates a random number from 10-20 or something like that?
Kinda like a random number generator... Thanks! |
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Unless you have a really non-compliant compiler you need to either put some scope resolution on cout (std::cout) or add a "using" directive. As a newbie, I will tell you that you need to test your input (cin) for success. If you aren't a newbie, then your code is just schlocky. I have no way to decide, at this point.
You might elucidate on the things you implemented to fix it. The forum is for the benefit of others who might have similar questions. It won't kill you to contribute, as opposed to just saying, "Oh, geez, Mom, I fixed it!!!". As for your second question, check the documentation for "rand" and apply some rudimentary math skills.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#6 |
|
Programmer
Join Date: Jul 2005
Posts: 66
Rep Power: 4
![]() |
Thank you, I will check it out.
|
|
|
|
|
|
#7 |
|
Programmer
Join Date: Jul 2005
Posts: 66
Rep Power: 4
![]() |
Okay, having some trouble, again.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//"Mage Wars" copyright Crimson Games 2007. All rights reserved.
//Created by Arnack, distributing this source is permited, as long
//as ownership is shown.
using namespace std;
int main(){
while (1 == 1) {
string name;
int ready;
int option;
int money;
int enemydmg;
int yourdmg;
srand ( time(NULL) );
enemydmg = rand() % 10 + 1;
yourdmg = rand() % 10 + 1;
//Opening
cout << "Hello there! What's your name?: \n";
getline (cin, name);
cout << "Welcome,";
cout << name;
cout << " , to Mage Wars! We are in a world of need, and we are hoping a true";
cout << "hero like you would help us! Please! Destroy the evil mages! \n\n";
cout << "Are you ready? 1: Yes 2: No \n";
cin >> ready;
} else if(ready == 2){
cout << "Come back when you are ready!";
} else {
cout << "Wrong reply!";
if (ready == 1){
//Main Game Starts Here
cout << "What would you like to do? \n";
cout << "1) Kill some evil mages! \n";
cout << "2) Buy some stuff! \n";
cout << "3) Check my status! \n";
cin >> option;
if (option == 1){
cout << "kill";
} else if(option == 2){
cout << "buy";
} else if(option == 3){
cout << "stats";
}
} else {
cout << "Wrong reply!";
}
cout << yourdmg << "you been attacked";
} else if(option == 2){
cout << "buy";
} else if(option == 3){
cout << "stats";
}
} else {
cout << "Wrong reply!";
}
system("PAUSE");
} |
|
|
|
|
|
#8 |
|
Professional Programmer
|
__________________
The world's first athletic computer geek! The home of PrProgramsStudios How not to post a question: <-- Please don't reply |
|
|
|
|
|
#9 |
|
Programmer
Join Date: Jul 2005
Posts: 66
Rep Power: 4
![]() |
Thanks Prm!
Okay, last question, pretty simple. How do I slow the time of when cout starts? Like I want 10 seconds before one cout is actually processed.. anyway? Last edited by Arnack; Aug 10th, 2007 at 9:37 PM. |
|
|
|
|
|
#10 | |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,034
Rep Power: 5
![]() |
Quote:
void sleep(int delay)
{
time_t start = time(NULL);
double delay_double = delay; // do the conversion once, rather than each iteration of the loop
while(difftime(time(NULL), start) < delay_double)
;
}
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot. - Vaarsuvius, Order of the Stick |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| A simple programming question | punter | C# | 8 | Jan 5th, 2006 4:04 PM |
| A simple question | Master | C++ | 9 | Dec 24th, 2005 2:20 PM |
| Question about multidimensional arrays of strings | aznluvsmc | C | 8 | Oct 15th, 2005 10:20 PM |
| Simple c# question | nez | C# | 8 | Jul 1st, 2005 6:29 PM |
| Simple (stupid cause I don't know it) basic question | massive-war | C++ | 17 | Apr 11th, 2005 11:03 PM |