Thread: Carrying ints a chars over to other functions.

  1. #31
    Registered User
    Join Date
    Nov 2006
    Posts
    31
    I changed around by code again to try and avoid what I think was happening (I was calling the function getAddy() in certain places unintentionally causing it to appear to just loop over and over again). I think I'm getting really close, but in the log file it only prints
    , , , , , , , ,
    and not the addies.
    BTW, I put comment codes around the part that makes sure it doesn't generate two exactly the same for because it was causing the program to freeze.
    Here's the code:
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <time.h>
    #include <cctype>
    #include <windows.h>
    #include <string.h>
    #include <fstream>
    #include <sstream>
    using namespace std;
    /*
    These constants define our upper
    and our lower bounds. The random numbers
    will always be between 1 and 6, inclusive.
    */
    const int LOW = 1;
    const int HIGH = 12;
    
    int main();
    string getAddy() // Get the email address from the user...pretty self-explanatory...
    {
           cout << "Enter address below: ";
           string addy;
           getline(cin,addy);
           verify:
           cout << "\nDid you enter \"" << addy << "\"? [y|n]: ";
           char yn;
           cin >> yn;
           cin.ignore();
           switch ( yn ) {
                  case 'y':
                       break;
                  case 'n':
                       cout << "\nPlease try again." << endl;
                       return getAddy();
                  default:
                          cerr << "\nInvalid selection. Please try again." << endl;
                          goto verify;
                  }
           return addy;
    }
    
    string randomize(string addy)
    {
         random:
         int len = addy.size(); // Get the length of the string
         string randAddy;                            //
    /*     string usedAddies;                          // 
         ifstream used_addies ("addies.txt");        // Makes sure that no two addies
         while (! used_addies.eof()) {               // generated are the same.
               getline(used_addies, usedAddies); }   //
         used_addies.close();                        //
      */   stringstream str_randAddy;                  //
         for (int i=1; i < len; i++) {
             if (rand() < HIGH * 0.15) {
                        randAddy[i] = toupper(addy[i]); // Capitilize some random characters
                        }
             }
    /*     str_randAddy << randAddy;
         if (usedAddies.find(str_randAddy.str(), 0) != std::string::npos) { // If it's already been made, start over
                                                 goto random; }
    */   cout << randAddy;
         return randAddy;
         
    }
    
    int main()
    {
        string addy1 = getAddy();
        cout << "How many addies are req'd? : "; // tells the program how many addies to generate
        int reqd;
        cin >> reqd;
        cin.ignore();
        for (int i=0; i < reqd; i++) {
        string addy = randomize(addy1);
         ofstream addies ("addies.txt", ios::app);
         if (addies.is_open()) {
            addies << addy << ", ";
            addies.close();
            }
            /*
            If there was a problem
            opening the file, terminate
            the program.
            */
         else {
              cerr << "Error: Could not open log file. Seconds until program terminates:\n\n";
              for (int i=10; i>0; i--) {
                  cout << i << "\r";
                  Sleep(1000);
                  }
              abort();
              }
             }
        cout << "Finished generating addies!!" << endl;
        system("PAUSE");
        return 0;
    }

  2. #32
    Registered User
    Join Date
    Nov 2006
    Posts
    31
    I think the problem might have something to do with passing the arguments to randomize(string), or what it returns. It could also be my use of fstream, any suggestions?

  3. #33
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    int main()
    {
        string addy1 = getAddy();
        cout << "How many addies are req'd? : "; // tells the program how many addies to generate
        int reqd;
        cin >> reqd;
        cin.ignore();
        for (int i=0; i < reqd; i++) {
        string addy = randomize(addy1);
         ofstream addies ("addies.txt", ios::app);
         if (addies.is_open()) {
            addies << addy << ", ";
            addies.close();
            }
    Well if only commas are being printed then addy (the blue line) must be an empty string. If that is the case then randomize() must have returned an empty string (the green line). If that is the case, it must have been passed an empty string; if that is the case then getAddy() must have returned an empty string. If that is the case then you likely entered an empty string.

    Unless . . .
    Code:
         string randAddy;                            //
    /*     string usedAddies;                          // 
         ifstream used_addies ("addies.txt");        // Makes sure that no two addies
         while (! used_addies.eof()) {               // generated are the same.
               getline(used_addies, usedAddies); }   //
         used_addies.close();                        //
      */   stringstream str_randAddy;                  //
         for (int i=1; i < len; i++) {
             if (rand() < HIGH * 0.15) {
                        randAddy[i] = toupper(addy[i]); // Capitilize some random characters
                        }
             }
    What if the highlighted if statement is false? randAddy[i] wouldn't be set to anything. I suggest putting
    Code:
    else randAddy[i] = addy[i];
    or initializing randAddy to addy.

    BTW you shouldn't use goto. You can get by with just loops.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #34
    Registered User
    Join Date
    Nov 2006
    Posts
    31
    ok, i've traced the problem to the value that randomize(string) returns. Just before it returns the value I added a cout, and it's not randomized, but thats another problem that I can deal with later...but then when it gets back to main I use cout again, and it shows up as blank. I even tried making randAddy a global variable, but to no avail.
    I will chagne the 'goto's eventually, but it is not at the top of my priotities at the moment. BTW, thanks so much to everyone who's been helping me !

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <time.h>
    #include <cctype>
    #include <windows.h>
    #include <string.h>
    #include <fstream>
    #include <sstream>
    using namespace std;
    /*
    These constants define our upper
    and our lower bounds. The random numbers
    will always be between 1 and 6, inclusive.
    */
    const int LOW = 1;
    const int HIGH = 12;
    string randAddy;
    
    int main();
    string getAddy() // Get the email address from the user...pretty self-explanatory...
    {
           cout << "Enter address below: ";
           string addy;
           getline(cin,addy);
           verify:
           cout << "\nDid you enter \"" << addy << "\"? [y|n]: ";
           char yn;
           cin >> yn;
           cin.ignore();
           switch ( yn ) {
                  case 'y':
                       break;
                  case 'n':
                       cout << "\nPlease try again." << endl;
                       return getAddy();
                  default:
                          cerr << "\nInvalid selection. Please try again." << endl;
                          goto verify;
                  }
           return addy;
    }
    
    string randomize(string addy)
    {
         cout << addy;
         random:
         int len = addy.size(); // Get the length of the string                          //
    /*     string usedAddies;                          // 
         ifstream used_addies ("addies.txt");        // Makes sure that no two addies
         while (! used_addies.eof()) {               // generated are the same.
               getline(used_addies, usedAddies); }   //
         used_addies.close();                        //
      */   stringstream str_randAddy;                  //
         for (int i=1; i < len; i++) {
             if (rand() < HIGH * 0.15) {
                        randAddy[i] = toupper(addy[i]); // Capitilize some random characters
                        }
             }
    /*     str_randAddy << randAddy;
         if (usedAddies.find(str_randAddy.str(), 0) != std::string::npos) { // If it's already been made, start over
                                                 goto random; }
    */   cout << randAddy << "\n";
         return randAddy;
         
    }
    
    int main()
    {
        string addy1 = getAddy();
        cout << "How many addies are req'd? : "; // tells the program how many addies to generate
        int reqd;
        cin >> reqd;
        cin.ignore();
        for (int i=0; i < reqd; i++) {
        string addy = randomize(addy1);
        cout << "rand " << addy << randAddy << "\n";
         ofstream addies ("addies.txt", ios::app);
         if (addies.is_open()) {
            addies << addy << ", ";
            addies.close();
            }
            /*
            If there was a problem
            opening the file, terminate
            the program.
            */
         else {
              cerr << "Error: Could not open log file. Seconds until program terminates:\n\n";
              for (int i=10; i>0; i--) {
                  cout << i << "\r";
                  Sleep(1000);
                  }
              abort();
              }
             }
        cout << "Finished generating addies!!" << endl;
        system("PAUSE");
        return 0;
    }

  5. #35
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Take my other suggestion and in randomize() go
    Code:
    string randAddy = addy;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #36
    Registered User
    Join Date
    Nov 2006
    Posts
    31
    ok, wen i do that, it returns the email as it was originally. this tells me that the problem lies in randomizing the characters. I will play around with it a bit.

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <time.h>
    #include <cctype>
    #include <windows.h>
    #include <string.h>
    #include <fstream>
    #include <sstream>
    using namespace std;
    /*
    These constants define our upper
    and our lower bounds. The random numbers
    will always be between 1 and 6, inclusive.
    */
    const int LOW = 1;
    const int HIGH = 12;
    
    int main();
    string getAddy() // Get the email address from the user...pretty self-explanatory...
    {
           cout << "Enter address below: ";
           string addy;
           getline(cin,addy);
           verify:
           cout << "\nDid you enter \"" << addy << "\"? [y|n]: ";
           char yn;
           cin >> yn;
           cin.ignore();
           switch ( yn ) {
                  case 'y':
                       break;
                  case 'n':
                       cout << "\nPlease try again." << endl;
                       return getAddy();
                  default:
                          cerr << "\nInvalid selection. Please try again." << endl;
                          goto verify;
                  }
           return addy;
    }
    
    string randomize(string addy)
    {
         cout << addy;
         string randAddy = addy;
         random:
         int len = addy.size(); // Get the length of the string                          //
    /*     string usedAddies;                          // 
         ifstream used_addies ("addies.txt");        // Makes sure that no two addies
         while (! used_addies.eof()) {               // generated are the same.
               getline(used_addies, usedAddies); }   //
         used_addies.close();                        //
      */   stringstream str_randAddy;                  //
         for (int i=1; i < len; i++) {
             if (rand() < HIGH * 0.15) {
                        randAddy[i] = toupper(addy[i]); // Capitilize some random characters
                        }
            else randAddy[i] = addy[i];
             }
             
    /*     str_randAddy << randAddy;
         if (usedAddies.find(str_randAddy.str(), 0) != std::string::npos) { // If it's already been made, start over
                                                 goto random; }
    */   cout << randAddy << "\n";
         return randAddy;
         
    }
    
    int main()
    {
        string addy1 = getAddy();
        cout << "How many addies are req'd? : "; // tells the program how many addies to generate
        int reqd;
        cin >> reqd;
        cin.ignore();
        for (int i=0; i < reqd; i++) {
        string addy = randomize(addy1);
        cout << "rand " << addy << "\n";
         ofstream addies ("addies.txt", ios::app);
         if (addies.is_open()) {
            addies << addy << ", ";
            addies.close();
            }
            /*
            If there was a problem
            opening the file, terminate
            the program.
            */
         else {
              cerr << "Error: Could not open log file. Seconds until program terminates:\n\n";
              for (int i=10; i>0; i--) {
                  cout << i << "\r";
                  Sleep(1000);
                  }
              abort();
              }
             }
        cout << "Finished generating addies!!" << endl;
        system("PAUSE");
        return 0;
    }

  7. #37
    Registered User
    Join Date
    Nov 2006
    Posts
    31
    i feel like i'm getting so close.
    here's my source code:
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <time.h>
    #include <cctype>
    #include <windows.h>
    #include <string.h>
    #include <fstream>
    #include <sstream>
    using namespace std;
    /*
    These constants define our upper
    and our lower bounds. The random numbers
    will always be between 1 and 6, inclusive.
    */
    const int LOW = 1;
    const int HIGH = 12;
    
    
    
    int main();
    string getAddy() // Get the email address from the user...pretty self-explanatory...
    {
           cout << "Enter address below: ";
           string addy;
           getline(cin,addy);
           verify:
           cout << "\nDid you enter \"" << addy << "\"? [y|n]: ";
           char yn;
           cin >> yn;
           cin.ignore();
           switch ( yn ) {
                  case 'y':
                       break;
                  case 'n':
                       cout << "\nPlease try again." << endl;
                       return getAddy();
                  default:
                          cerr << "\nInvalid selection. Please try again." << endl;
                          goto verify;
                  }
           return addy;
    }
    
    string randomize(string addy)
    {
           
           int randNum;
           time_t seconds;
            /*
            Get value from system clock and
            place in seconds variable.
            */
            time(&seconds);
            /*
            Convert seconds to a unsigned
            integer.
            */
            srand((unsigned int) seconds);
            randNum = rand() % (HIGH - LOW + 1) + LOW;
            cout << randNum << endl;
            Sleep(1000);
            
            
         cout << addy;
         string randAddy = addy;
         random:
         int len = addy.size(); // Get the length of the string                          //
    /*     string usedAddies;                          // 
         ifstream used_addies ("addies.txt");        // Makes sure that no two addies
         while (! used_addies.eof()) {               // generated are the same.
               getline(used_addies, usedAddies); }   //
         used_addies.close();                        //
      */   stringstream str_randAddy;                  //
         for (int i=1; i < len; i++) {
             if (randNum < 6) {
                        randAddy[i] = toupper(addy[i]); // Capitilize some random characters
                        }
            else randAddy[i] = addy[i];
             }
             
    /*     str_randAddy << randAddy;
         if (usedAddies.find(str_randAddy.str(), 0) != std::string::npos) { // If it's already been made, start over
                                                 goto random; }
    */   cout << randAddy << "\n";
         return randAddy;
         
    }
    
    
    int main()
    {
    
        string addy1 = getAddy();
        cout << "How many addies are req'd? : "; // tells the program how many addies to generate
        int reqd;
        cin >> reqd;
        cin.ignore();
        for (int i=0; i < reqd; i++) {
        string addy = randomize(addy1);
        cout << "rand " << addy << "\n";
         ofstream addies ("addies.txt", ios::app);
         if (addies.is_open()) {
            addies << addy << ", ";
            addies.close();
            }
            /*
            If there was a problem
            opening the file, terminate
            the program.
            */
         else {
              cerr << "Error: Could not open log file. Seconds until program terminates:\n\n";
              for (int i=10; i>0; i--) {
                  cout << i << "\r";
                  Sleep(1000);
                  }
              abort();
              }
             }
        cout << "Finished generating addies!!" << endl;
        system("PAUSE");
        return 0;
    }
    and here's what it looks like when I run it...

    http://i3.tinypic.com/2z5ohoy.jpg

  8. #38
    Registered User
    Join Date
    Nov 2006
    Posts
    31
    Do you tink it might have something to do with the for statement in randomize? I meed to get a new random number each time. I feel like I'm getting really close!

  9. #39
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Here's your problem. (Don't you love it when someone says that? )
    Code:
         for (int i=1; i < len; i++) {
             if (randNum < 6) {
                        randAddy[i] = toupper(addy[i]); // Capitilize some random characters
                        }
            else randAddy[i] = addy[i];
             }
    Since you start counting at 1, randAddy[0] will be 0 (the default value), making the string an empty one. Start counting at 0, because that's what you in C++.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #40
    Registered User
    Join Date
    Nov 2006
    Posts
    31
    YAY!!! Thanks so much dwks, and everyone else who helped me! My program is finally funtioning!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  2. writing strings chars from ints
    By deleeuw in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2003, 09:09 AM
  3. fancy strcpy
    By heat511 in forum C++ Programming
    Replies: 34
    Last Post: 05-01-2002, 04:29 PM
  4. Chars - ints
    By MethodMan in forum C Programming
    Replies: 2
    Last Post: 04-15-2002, 08:22 PM
  5. converting chars to ints
    By nebie in forum C++ Programming
    Replies: 6
    Last Post: 09-01-2001, 11:33 AM