spechal.com | [spesh-uhl]

CAT | C++

358 views
Nov/09

16

C++ Read from a file using fstream

/**
  Read from a file (version 1)
  Copyright 2009 Travis Crowder
  travis.crowder@spechal.com
  Published under the MIT License
*/
#include <iostream>
#include <fstream>
#include <string>

int main(int argc, char* argv[]){

  // create the file handle, opening the file
  std::fstream myFile("text.txt", std::ios::in);

  // create a string to hold the line
  std::string line;

  if(myFile.is_open()){
    while(!myFile.eof()){
      getline(myFile, line);
      std::cout << line << std::endl;
    }
  } else {
    std::cout << "Could not open text.txt for reading." << std::endl;
    return -1;
  }

  return 0;
}

202 views
Nov/09

16

C++ Write to a file using fstream

/**
  Write to a file (version 1)
  Copyright 2009 Travis Crowder
  travis.crowder@spechal.com
  Published under the MIT License
*/
#include <iostream>
#include <fstream>

int main(int argc, char* argv[]){

  // create the file handle, opening the file as well
  std::fstream myFile("text.txt", std::ios::out);

  if(myFile.is_open()){
    myFile << "I am just some text in some file.";
    myFile.close();
    std::cout << "File written to." << std::endl;
  } else {
    // could not create/write to file
    std::cout << "Could not write to file." << std::endl;
    return -1;
  }

  return 0;
}

No tags

397 views
Nov/09

16

C++ Palindrome Checker

/**
  Palindrome Checker
  Copyright 2009 Travis Crowder
  Published under the MIT License
*/

#include <iostream>
#include <algorithm>
#include <string>

bool isPalindrome(const std::string theWord);

int main(int argc, char* argv[]){

  std::string theWord;

  if(!argv[1]){
    std::cout << "Enter the word to check: ";
    std::cin >> theWord;
  } else {
    theWord = argv[1];
  }
  std::cout << std::endl;

  /**
    Make all of the characters lower case
  */
  int i = 0;
  while(theWord[i]){
    if(isupper(theWord[i]))
      theWord[i] = tolower(theWord[i]);
    i++;
  }

  std::cout << theWord << " is ";
  if(!isPalindrome(theWord))
    std::cout << "NOT ";
  std::cout << "a palindrome." << std::endl;
  return 0;
}

bool isPalindrome(const std::string theWord){
  std::string tmp = theWord;
  reverse(tmp.begin(), tmp.end());
  if(tmp == theWord)
    return true;
  return false;
}

123 views
Nov/09

15

C++ Simple Temperature Converter

/**
  Temperature Converter
  Copyright 2009 Travis Crowder
  travis.crowder@spechal.com
  Published under the MIT License
*/

#include <iostream>

int main(int argc, char* argv[]){

  /**
    Create a variable to hold the input temperature value
  */
  double input = NULL;

  /**
    Create a variable to hold the output temperature value
  */
  double output = 0;

  /**
    Create a variable to hold which type we are converting from
  */
  char convFrom;
  /**
    Create a variable to hold which type we are converting to
  */
  char convTo;

  while(input == NULL){
    std::cout << "Enter the source temperature, without the type: ";
    std::cin >> input;
    std::cout << std::endl;
  }

  while(convFrom != 'C' && convFrom != 'F'){
    std::cout << "What is the source temperature type?" << std::endl;
    std::cout << "[C]entigrade" << std::endl;
    std::cout << "[F]ahrenheit" << std::endl;
    std::cin >> convFrom;
  }

  /**
    Output the results
  */
  std::cout << "Your converted temperature is: ";
  if(convFrom == 'F'){
    output = (input - 32) * 5 / 9;
    convTo = 'C';
  } else {
    output = (input * 9 / 5) + 32;
    convTo = 'F';
  }
  std::cout << output << convTo << std::endl;

  return 0;
}

No tags

125 views
Nov/09

15

C++ Guessing Game

/**
  Guessing Game
  Copyright 2009 Travis Crowder
  travis.crowder@spechal.com
  Published under the MIT License
*/
#include <iostream>

int main(int argc, char* argv[]){

  /**
    Create our variable to hold the guess made
  */
  int guess = 0;

  /**
    Let's keep track of the number of guesses made.
  */
  int guesses = 0;

  /**
    Set the secret to be a random number between 1 and 100
    Remember to seed the random number generator
  */
  srand(time(0));
  int secret = rand() % 100 + 1;

  /**
    while our guess is not the secret, get a guess
  */
  while(guess != secret){
    std::cout << "Enter your guess, between 1 and 100: ";
    std::cin >> guess;
    guesses++;
    if(guess == secret){
      /** guess was correct */
      std::cout << "You've figured out the secret in " << guesses;
      if(guesses == 1)
        std::cout << " guess";
      else
        std::cout << " guesses";
      std::cout << "!  It was " << secret << "!" << std::endl;
    } else if(guess > secret){
      /** guess was too high */
      std::cout << "Your guess was too high!  Try again!" << std::endl;
    } else {
      /** our guess can only be lower, logically */
      std::cout << "Your guess was too low!  Try again!" << std::endl;
    }
  }

  return 0;
}

No tags

Find it!

Theme Design by devolux.org