C++ Learning Community Forum
September 11, 2010, 01:03:10 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Hello. Smiley
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: The Number 23!  (Read 765 times)
Shonoby
Programmer
Dr. of C++ology
****
Posts: 659


Pixel Artist


View Profile
« on: October 10, 2007, 03:24:37 AM »

WELL kiddies the other day i watched the move 'THE NUMBER 23' and i loved it so i wanted to know if my name was equal to the 23. So i created a little program did just that, and well my name does not equal 23. So try your luck kiddies:
*NOTE i started to comment on my codes, which i had never done before LOL
Code:
// This program calculates the value( in decimal form ) of a given word

// Word's Value
// By: Darwin Aguirre
#include <iostream> // library "iostream" for basic input/output

using std::cout; // user declaration for std::cout
using std::cin; // user declaration for std::cin
using std::endl; // user declaration for std::endl
using std::right; // user declaration for std::right

#include <iomanip> // library "iomanip" for manipulating input/output

using std::setw; // user declaration for std::setw

#include <cctype> // library "cctype" for C-style manipulation

#include <string> // library "string" for C++ style strings

using std::string; // user declaration for std::string

int CalculateValue( const string & ); // prototype function to determine value of word
void PrintInformation( const int &, const string & ); // prototype function to print information of the word

int main() // function main() to start the code
{ // start of main():
string Word; // variable of type string called Word

// introduction and details of the program
cout << "Welcome to this program, called \"Word's Value\".\n"
<< "What this program does is calculate the value"
     << "( in decimal form ) of a given word" << endl;

// instructions and promp
cout << "Please input a word that you would like to find the value of:" << endl;

Word.resize( 21 ); // resizeing the variable so data can be inserted

// setw( 21 ) specifies that cin should only read a max of 20 characters into
// Word and ignore the rest
cin >> setw( 21 ) >> Word;

// print the information of the word, notice that CalculateValue() is called
// as a argument for this function, so that it gets the value of the word
// via another function, also takes a string
PrintInformation( CalculateValue( Word ), Word );

// pause the system
system( "PAUSE" );
return 0; // return successful termination
} // ---------------

// definition of function CalculateValue()
int CalculateValue( const string &word )
{ // start of CalculateValue():
int value = 0; // variable of type int to store the word's value
// store the alphabet in an array so comparisons can be done
char Alpha[ 26 ] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
'y', 'z' };
// create all the characters lowercase if any( via loop ):
for ( size_t a = 0; a < word.size(); a++ ) {
// determine if the word is uppercase
if ( isupper( word.at( a ) ) == true ) {
// if so change it to lowercase
toupper( word.at( a ) );
}
}
// loop through all the elements of word to determine the value
for ( size_t i = 0; i < word.size(); i++ ) {
// loop the the alphabet to compare, then add
for ( int x = 0; x < 27; x++ ) {
// if they do match
if ( word.at( i ) == Alpha[ x ] )
value += ( x + 1 ); // add x + 1, because value += 0 don't work
} // end loop for alphabet
} // end loop for the word

return value; // return the value of the word
} // -------------------

// definition for function PrintInformation()
void PrintInformation( const int &Wvalue, const string &word )
{ // start of PrintInformation():
// set up a chart to display information
cout << "Word:" << setw( word.size() + 16 ) << right << "Value:" << endl;

// display the words information
cout << word << setw( word.size() + 11 ) << right << Wvalue << endl;
} // --------------
Post any words that equal 23 or whatever i dont care.
Logged

Reality is what we make of it, so by definition we're all living in a fantasy.
- Shonoby
ih8censorship
Megalomaniac!!!
Administrator
C++ guru
*****
Posts: 1241



View Profile
« Reply #1 on: October 10, 2007, 03:47:06 AM »

bat==23
 Grin

Logged

PC==perfect_companion

Knowledge cannot come packaged and predigested; it must be chewed over carefully before swallowed.

What have you tried?
Shonoby
Programmer
Dr. of C++ology
****
Posts: 659


Pixel Artist


View Profile
« Reply #2 on: October 10, 2007, 03:49:21 AM »

Really, OMG bats are EVIL!
No wounder bats = vampires!
Logged

Reality is what we make of it, so by definition we're all living in a fantasy.
- Shonoby
adeyblue
Dr. of C++ology
****
Posts: 653

Taming the turntables a beat at a time


View Profile WWW
« Reply #3 on: October 11, 2007, 02:18:48 PM »

Now that you're writing comments, you better learn to read them too:
Quote
      // determine if the word is uppercase
      if ( isupper( word.at( a ) ) == true ) {
         // if so change it to lowercase
         toupper( word.at( a ) ); // this doesn't actually change anything either
      }

Because I'm feeling extra evil today, here's a 23 line version (not counting includes)
Code
GeSHi (cpp):
#include <iostream>
#include <string>
#include <numeric>
#include <cctype>
#include <algorithm>
#include <functional>
 
char CharToAlphaPosition(char ch) {
   return (std::tolower(ch) - 'a') + 1;
}
 
int CalculateWord(std::string& word) {
   word.erase( std::remove_if( word.begin(), word.end(), std::not1( std::ptr_fun( std::isalpha ) ) ), word.end() );
   std::transform(word.begin(), word.end(), word.begin(), &CharToAlphaPosition);
   return std::accumulate(word.begin(), word.end(), 0);
}
 
int main() {
   std::cout << "Let's calculate the value of a word shall we?\n";
   char ch = 'Y';
   while(ch == 'Y')
   {
       std::string word;
       std::cout << "Enter the magic word: "; std::cin >> word;
       int val = CalculateWord(word); std::cout << "That comes to: " << val << '\n';
       std::cout << (val == 23 ? "That's an evil word, maybe even vile\n" : "Bah, that's not evil at all\n");
       std::cout << "To go again enter Y: ";
       std::cin >> ch; ch = std::toupper(ch);
   }
}
 
Created by GeSHI 1.0.7.18
Logged

syazhani
I wonder how long can titles be... because the longer it is the more attention I can get!
Dr. of C++ology
****
Posts: 529


Cats > Dogs


View Profile
« Reply #4 on: October 11, 2007, 08:37:20 PM »

Code:
        std::cout << "Enter the magic word: "; std::cin >> word;
        int val = CalculateWord(word); std::cout << "That comes to: " << val << '\n';
        std::cout << (val == 23 ? "That's an evil word, maybe even vile\n" : "Bah, that's not evil at all\n");
        std::cin >> ch; ch = std::toupper(ch);

You cheated on those 4 lines. Code's void.  Angry
Logged

"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it."- Brian W. Kernighan

What people misunderstood about Islam presented in comical way: http://ummahfilms.com
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!