-
Little help?
Hello, i'm a long time lurker, first time posting...
i have 2 problems really,
This next block of code is part of a sorta, dice roller thats rolls 7 20 sided dice, but only rolls numbers between 10 and 20. what i want is to put each roll into a variable to be used later in the program
Code:
high = 20;
cin.get();
cout << "Here are your rolls: " << endl;
for (int x = 0; x < 7; x++) {
die = rand() % (high - 10 + 1) + 10;
cout<< die << endl;
}
cout << endl;
my next thing is, for some reason cin.getline(); is not working.. here's the code i used
Code:
char backstory[2000];
cout << "Please enter your your character's backstory, if any: ";
cin.getline(backstory, 2000, "/n");
the backstory variable is decalred on the top of the code, while the next part is near the end. when i run the program it works fine up to any cin.getline().. it actually skips it and moves on to the next line
it lookes like this:
Code:
Please enter your character's first name: [userinput]
Please enter your character's last name: [userinput]
Please enter your character's backstory: [this part is skippped and goes to next line]
Please press any key to continue....
Any ideas on both problems?
-
1. Only the last roll will be stored in the die variable once the for loop breaks; use an array of ints so you can access all the numbers randomly generated.
2. The delimeter for getline() must be a char '\n' not a string "\n". As it is the default delimeter, you don't even need to type it
-
i tried the std::getline() before you edited, and me type "\n" was a typo here.. in the code, it is '\n'.. std::getline(backstory,2000,'\n') did not work at all said no matching function
The dice rolling worked well, btw
-
Use cin.get(backstory, 2000);
I'm very sorry, I misread before. :D
-
Just to confirm, you tried:
Code:
std::cin.getline(backstory, 2000);
right?
-
ok, so, it kinda worked, when i went tried when i entered the "user input" part a, it kinda took off the first word
Code:
Enter Backstory: {user input} = "This is a long backstory"
it displayed
" is a long backstory"
edit:
yes i did use
Code:
std::getline(backstory, 2000);
Edit2 ok i see that part where i messed it.. i was missing cin. on the std:: but then i tried that with full line.. and it still skips the first word
-
Code:
char backstory[2000];
std::cout << "Please enter your your character's backstory, if any: ";
std::cin.getline(backstory, 2000, '\n');
std::cout<<backstory<<"\n";
std::cin.get();
// OUTPUT:
// Please enter your character's backstory, if any: This is a really long story.
// This is a really long story.
-
I'm using
Code:
using namespace std;
other than using 'std::' the rest of the code looks exaclty like i have it
with few things in between here and there...
If you haven't guessed already, this a prgram designed to make a RP character sheet faster without having to write it down on paper. just print and done.. but anyway... i realized why it was skiping the first word i had 'cin >> backstory' before the getline code, which brings it back to the problem i was having before, it totally skips the user input part and goes to the next line of code which would be
Code:
cout << "Generating Data..." << endl;
system("pause");
i'm using the pasue command because the code also wants to skip cin.get()
-
Ah! now I understand... it sounds to me like you need to flush the input buffer, so just type Code:
cin.ignore(10000, '\n');
before the code block you are having problems. Now, it should work, and you don't have to use system("PAUSE");
-
HA! Thanks alot, that worked perfectly!
Edit: ok, one last thing, after generating data...., and since i don't have to use system pasue.. i do want it to pause for a certain amout of seconds.. and then continue with program.. can this be done?
-
Yes, but this is rather implementation-specific. The Windows API provides Sleep(). I remember there being a quasi-standard header (<unistd.h> IIRC) containing usleep() which works similiar to Sleep().Or try here.
-
Code:
#include <ctime> // Standard: for clock stuff
void wait (double seconds)
{
// using namespace std; // when necessary, define the namespace
clock_t start_time = clock();
while((clock() - start_time) < seconds * CLOCKS_PER_SEC)
{
// do nothing
}
}
Just call that function there. You can wait as long or as short as you like, even down to fractions of seconds. It's a lot more standard than what the person above me was saying.
-
Thanks, that worked, though now i have ran into another prblem with the program
Code:
ofstream cs_file("characters\\"cfullname".html");
What thats suppose to do is create a .html file with the 'characters' first and last name combine and will put all the info in that file.. "cfirstname-clastname.html" with other blocks of code after that.. but the problem is that block of code above, won't work. any ides?
-
Code:
string fname;
string lname;
string filename;
cout << "Enter first name: ";
getline(cin,fname);
cout << "Enter last name: ";
getline(cin,lname);
filename = fname + lname + ".txt";
ofstream output(filename.c_str() );
If you enter in "Bob" and "Dole" for the first and last names, then the program above will attempt to open a file called "BobDole.txt". That should give you a clue how to proceed.
-
Code:
char cfirstname[100];
char clastname[100];
char cfullname[200];
string filename;
cfullname[0] = '\0';
strcat ( cfullname, cfirstname );
strcat ( cfullname, "-" );
strcat ( cfullname, clastname );
filename = "characters\\" + cfullname +".html";
The output file is suppose to go to the folder Characters and atempt to make the first and last name file like "characters\Bob-Dole.html"
when i try to compile it the error reads
Code:
invalid operands of types `const char[12]' and `char[200]' to binary `operator+'
--- on # line
(filename = "characters\\" + cfullname +".html";)
now thats before the code to create the file