Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Oppg2.cpp
- //
- #include <iostream> // C++-bibliotek, filendelsen h er tatt bort
- #include <cstring> // C-bibliotek, filendelsen er borte, det er...
- #include <cctype> // ... kommet en c først i navnet
- #include <fstream>
- #include <cstdlib>
- using namespace std; // bruker standard navnerom
- //char *strpbrk(const char *letI, const char *letEtter);
- const char filnavn[] = "Oppg2_txt.txt";
- char tekst[20];
- const char tall[] = "0123456789";
- int sum = 0;
- int main(){
- ifstream innfil;
- innfil.open(filnavn);
- if (!innfil) {
- cout << "Feil ved åpning av innfil." << endl;
- exit(EXIT_FAILURE);
- }
- int i=0;
- while (!innfil.eof()) { // leser fram til filslutt
- tekst[i++] = innfil.get();
- }
- char * temp;
- temp = strpbrk (tekst, tall);
- while (temp != NULL){
- char test = temp[0];
- char *testref = &test;
- sum += atoi(testref);
- temp = strpbrk (temp+1,tall);
- }
- cout << '\n' << sum << endl;
- system("pause"); // Pauser så man kan se output
- return 0; // nødvendig pga at main() er av typen int
- }
Add Comment
Please, Sign In to add comment