Guest User

Untitled

a guest
Apr 24th, 2018
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. #include <string>
  4. #include <sstream>
  5. #include <fstream>
  6.  
  7.  
  8. int main()
  9. {
  10.     std::map<char, int> alphabet;
  11.     std::string alphString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  12.     int stringOfNumbers = 1;
  13.     int iterations = 0;
  14. // put every letter of the alphabet into std::map
  15.     for(int i = 0; i != alphString.length(); i ++){
  16.         alphabet.insert(std::pair<char, int>(alphString[i], i+1));
  17.     }
  18.     for(std::map<char, int>::iterator it = alphabet.begin(); it != alphabet.end(); it ++){
  19.         std::cout << it->first << " => " << it->second << std::endl;
  20.     } std::cout << std::endl;
  21.  
  22. // first file open to read number of lines (there is 7 lines)
  23.     std::fstream file("test.txt");
  24.     int countLines = 0;
  25.     if(file.is_open()){
  26.         std::string word;
  27.         while(getline(file, word)){
  28.             std::istringstream iss(word);
  29.             while(iss){
  30.                 std::string s;
  31.                 iss >> s;
  32.                 countLines++;
  33.             }
  34.         }
  35.         file.close();
  36.     }
  37. // second file open to read character by character and calculate
  38.     std::fstream file2("test.txt");
  39.     if(file2.is_open()){
  40.         char singleLetter;
  41.         int sum = 0;
  42.         while(file2.get(singleLetter)){
  43.             std::cout << alphabet.find(singleLetter)->first << " => " << alphabet.find(singleLetter)->second << std::endl;
  44. // sum the values of the single chars for the whole word
  45.             sum += alphabet.find(singleLetter)->second;
  46.         } std::cout << "Sum: " << sum << std::endl;
  47. // I musted divide by 2, because lines were counted somehow bad
  48.         std::cout << "Lines: " << countLines/2 << std::endl;
  49.         file2.close();
  50.     } std::cout << std::endl;
  51. // first ten elements of the string of numbers
  52.     for(int i = 2; iterations != 10; i ++){
  53.         stringOfNumbers += i;
  54.         std::cout << stringOfNumbers << " ";
  55.         iterations++;
  56.     }
  57.  
  58.  
  59.  
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment