Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Simple question! Strings! (http://www.programmingforums.org/showthread.php?t=13744)

Arnack Aug 10th, 2007 7:59 PM

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!";

By the way: the error points out towards "cout string;"
Thanks!
Arnack

Jessehk Aug 10th, 2007 8:20 PM

replace

:

cout << string;

with
:

cout << name;

Name refers to the variable name, "string" refers to the type of the variable.

Arnack Aug 10th, 2007 8:24 PM

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

Arnack Aug 10th, 2007 8:52 PM

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!

DaWei Aug 10th, 2007 8:54 PM

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.

Arnack Aug 10th, 2007 9:07 PM

Thank you, I will check it out.

Arnack Aug 10th, 2007 9:45 PM

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");

}

ACtually, I forgot the main include iostream... i'm full of mistakes..

Prm753 Aug 10th, 2007 9:52 PM

Read this:

http://www.cppreference.com/cppstring/index.html

Hope that helps. :)

Arnack Aug 10th, 2007 10:25 PM

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?

lectricpharaoh Aug 10th, 2007 10:54 PM

Quote:

Originally Posted by Arnack
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?

You need to use some kind of a delay. There is no sleep() function in standard C++, but you can roll your own. Simply store the current time in a time_t variable, then loop until the current time is x seconds greater. This should work:
:

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)
    ;
}

The downside to this approach is that it, like any tight waiting loop, is needlessly demanding on the CPU. You should see if your OS supports some kind of sleep() function. Generally, such a function will put the thread to sleep for the requested duration, rather than consume CPU cycles while it's idling.


All times are GMT -5. The time now is 3:10 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC