Thread: Little help?

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    9

    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?

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    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
    Last edited by whiteflags; 05-03-2006 at 11:11 PM.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    9
    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
    Last edited by SilverWolfe; 05-03-2006 at 11:26 PM.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Use cin.get(backstory, 2000);

    I'm very sorry, I misread before.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Just to confirm, you tried:
    Code:
    std::cin.getline(backstory, 2000);
    right?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    9
    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
    Last edited by SilverWolfe; 05-03-2006 at 11:39 PM.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    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.

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    9
    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()

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    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");

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    9
    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?
    Last edited by SilverWolfe; 05-04-2006 at 12:17 AM.

  11. #11
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    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 <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    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.
    Last edited by whiteflags; 05-04-2006 at 02:19 AM.

  13. #13
    Registered User
    Join Date
    May 2006
    Posts
    9
    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?

  14. #14
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    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.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  15. #15
    Registered User
    Join Date
    May 2006
    Posts
    9
    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

Popular pages Recent additions subscribe to a feed