Guest User

Untitled

a guest
Jun 23rd, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1. //
  2. // Oppg2.cpp
  3. //
  4. #include <iostream> // C++-bibliotek, filendelsen h er tatt bort
  5. #include <cstring>  // C-bibliotek, filendelsen er borte, det er...
  6. #include <cctype>   // ... kommet en c først i navnet
  7. #include <fstream>
  8. #include <cstdlib>
  9. using namespace std;  // bruker standard navnerom
  10.  
  11. //char *strpbrk(const char *letI, const char *letEtter);
  12.  
  13. const char filnavn[] = "Oppg2_txt.txt";
  14. char tekst[20];
  15. const char tall[] = "0123456789";
  16. int sum = 0;
  17.  
  18. int main(){
  19.  
  20.   ifstream innfil;
  21.   innfil.open(filnavn);
  22.   if (!innfil) {
  23.     cout << "Feil ved åpning av innfil." << endl;
  24.     exit(EXIT_FAILURE);
  25.   }
  26.  
  27.   int i=0;
  28.   while (!innfil.eof()) {  // leser fram til filslutt
  29.     tekst[i++] = innfil.get();
  30.   }
  31.  
  32.  
  33.   char * temp;
  34.   temp = strpbrk (tekst, tall);
  35.  
  36.   while (temp != NULL){
  37.     char test =  temp[0];
  38.     char *testref = &test;
  39.     sum += atoi(testref);
  40.     temp = strpbrk (temp+1,tall);
  41.   }
  42.  
  43.  
  44.  cout << '\n' << sum << endl;
  45.  
  46.   system("pause"); // Pauser så man kan se output
  47.     return 0; // nødvendig pga at main() er av typen int
  48. }
Add Comment
Please, Sign In to add comment