Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 10th, 2007, 6:59 PM   #1
Arnack
Programmer
 
Join Date: Jul 2005
Posts: 66
Rep Power: 4 Arnack is on a distinguished road
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
__________________
Ack Network
*UNDER CONSTRUCTION*
Now hiring staff of all sorts.
Arnack is offline   Reply With Quote
Old Aug 10th, 2007, 7:20 PM   #2
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4 Jessehk is on a distinguished road
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!
Jessehk is offline   Reply With Quote
Old Aug 10th, 2007, 7:24 PM   #3
Arnack
Programmer
 
Join Date: Jul 2005
Posts: 66
Rep Power: 4 Arnack is on a distinguished road
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
__________________
Ack Network
*UNDER CONSTRUCTION*
Now hiring staff of all sorts.
Arnack is offline   Reply With Quote
Old Aug 10th, 2007, 7:52 PM   #4
Arnack
Programmer
 
Join Date: Jul 2005
Posts: 66
Rep Power: 4 Arnack is on a distinguished road
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!
__________________
Ack Network
*UNDER CONSTRUCTION*
Now hiring staff of all sorts.
Arnack is offline   Reply With Quote
Old Aug 10th, 2007, 7:54 PM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Aug 10th, 2007, 8:07 PM   #6
Arnack
Programmer
 
Join Date: Jul 2005
Posts: 66
Rep Power: 4 Arnack is on a distinguished road
Thank you, I will check it out.
__________________
Ack Network
*UNDER CONSTRUCTION*
Now hiring staff of all sorts.
Arnack is offline   Reply With Quote
Old Aug 10th, 2007, 8:45 PM   #7
Arnack
Programmer
 
Join Date: Jul 2005
Posts: 66
Rep Power: 4 Arnack is on a distinguished road
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..
__________________
Ack Network
*UNDER CONSTRUCTION*
Now hiring staff of all sorts.
Arnack is offline   Reply With Quote
Old Aug 10th, 2007, 8:52 PM   #8
Prm753
Professional Programmer
 
Prm753's Avatar
 
Join Date: Oct 2005
Location: United States
Posts: 447
Rep Power: 4 Prm753 is on a distinguished road
Send a message via AIM to Prm753 Send a message via MSN to Prm753
Read this:

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

Hope that helps.
__________________
The world's first athletic computer geek!
The home of PrProgramsStudios
How not to post a question: <-- Please don't reply
Prm753 is offline   Reply With Quote
Old Aug 10th, 2007, 9:25 PM   #9
Arnack
Programmer
 
Join Date: Jul 2005
Posts: 66
Rep Power: 4 Arnack is on a distinguished road
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?
__________________
Ack Network
*UNDER CONSTRUCTION*
Now hiring staff of all sorts.

Last edited by Arnack; Aug 10th, 2007 at 9:37 PM.
Arnack is offline   Reply With Quote
Old Aug 10th, 2007, 9:54 PM   #10
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,034
Rep Power: 5 lectricpharaoh will become famous soon enough
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.
__________________
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
lectricpharaoh is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:24 AM.

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