Avatar billede Slettet bruger
28. november 2005 - 17:07 Der er 17 kommentarer og
1 løsning

Simpelt spørgsmål - hurtige points

Hej eksperter!
Jeg er lidt grøn i det, og sidder med en lille kode her:

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string name;
    int age;

    cout << "Variables, input, and output\n\nPlease enter your first name and press \<ENTER\>.\nName: ";
    cin >> name;

    cout << "\nPlease enter your age and press \<ENTER\>.\nAge: ";
    cin >> age;

    cout << "\nHello " << name << "!\nYou are " << age << " years old!";

    cin.get();
}

Når jeg har indtastet min alder og trykker ENTER forsvinder boksen...
Er det noget med, at den ikke venter input?
Avatar billede mathiash Nybegynder
28. november 2005 - 17:30 #1
#include "stdafx.h"
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;

int main()
{
    string name;
    int age;

    cout << "Variables, input, and output\n\nPlease enter your first name and press \<ENTER\>.\nName: ";
    cin >> name;

    cout << "\nPlease enter your age and press \<ENTER\>.\nAge: ";
    cin >> age;

    cout << "\nHello " << name << "!\nYou are " << age << " years old!\r\n";

    system("PAUSE");
}

Er nok den nemmeste måde at gøre det på.
Avatar billede Slettet bruger
28. november 2005 - 18:55 #2
Ok... Tester det lige...
Avatar billede Slettet bruger
28. november 2005 - 18:59 #3
Hehee...!
Så virker det... Kan man vel godt sige, men...
Jeg vil jo bare holde vinduet obent; ikke have in tekst frem om at trykke en tast for at lukke... Kan jeg ikke bare få vinduet til at blive åbent, så jeg selv kan skrive en tekst og brugeren derefter kan lukke vinduet ned - UDEN den indbyggede meddelelse?
Avatar billede bertelbrander Novice
28. november 2005 - 19:30 #4
Problemet er at der hænger et linieskift i keyboard bufferen, og de er ualmindeligt svære at få ud.

Man kunne løse det med:
#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <string>
#include <stdlib.h>
using namespace std;

template <typename T>
bool Get(T &t, istream &is = cin)
{
  string s;
  getline(is, s);
  stringstream ss(s);
  return ss >> t;
}
int main()
{
    string name;
    int age;

    cout << "Variables, input, and output\n\nPlease enter your first name and press <ENTER>.\nName: ";
    getline(cin, name);

    cout << "\nPlease enter your age and press <ENTER>.\nAge: ";
    Get(age);

    cout << "\nHello " << name << "!\nYou are " << age << " years old!\r\n";
    cin.get();
}

Bemærk at jeg bruger getline til navn, så kan der også være mellemrum.
Man skal ikke have \ foran <> i "string literals"
Avatar billede Slettet bruger
28. november 2005 - 20:09 #5
Det fik jeg ikke helt fat i...
Men hvorfor dur dette ikke?:

#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <string>
#include <stdlib.h>
using namespace std;

    template <typename T>
bool Get(T &t, istream &is = cin) {
    string s;
    getline(is, s);
    stringstream ss(s);
    return ss >> t;
}
int main() {
    string name;
    int rating;
    string comment;

    cout << "Closing window\n\nHow would you rate C++ from 1 to 5?\nPlease type your ansver and press \<ENTER\>.\nRating: ";
    Get(cin, rating);

    cout << "\n\nPlease enter a comment and press \<ENTER\>.\nComment: \n\n";
    getline(comment);

    if(rating < 0) {
        cout << "Whoa! You really don't like C++...\nYou rated it " << rating << "!\nYou also wrote:\n" << comment << "\nInteresting...";
    } else if(rating == 0) {
        cout << "I see you really don't like C++...\nYou rated it " << rating << "!\nYou also wrote:\n" << comment << "\nInteresting...";
    } else if(rating == 1) {
        cout << "I see you  don't like C++...\nYou rated it " << rating << ".\nYou also wrote:\n" << comment << "\nHmm...";
    } else if(rating > 1 && rating < 5) {
        cout << "Ok...\nYou rated it " << rating << "...\nYou also wrote:\n" << comment << "\nPretty normal...";
    } else if(rating == 5) {
        cout << "I see you like C++...\nYou rated it " << rating << "!\nYou also wrote:\n" << comment << "\nI like C++ too!";
    } else if(rating > 5) {
        cout << "I see you really like C++!\nYou rated it " << rating << "!\nYou also wrote:\n" << comment << "\nI like C++ too!";
    } else {
        cout << "There must be some mistake...\nYou rated it " << rating << "!\You also wrote:\n" << comment << "\nWierd...";
    }
    cout "\n\nPress \<ENTER\> to close this window.";
    cin.get();
}
Avatar billede bertelbrander Novice
28. november 2005 - 20:14 #6
Get(rating, cin);
Eller blot:
    Get(rating)

Og:
    getline(cin, comment);

(det er måske ulogisk, du kan bytte argumenterne til Get)

Og
    cout << "\n\nPress \<ENTER\> to close this window.";

Og du skal ikke have \ foran < og >
Avatar billede Slettet bruger
28. november 2005 - 21:08 #7
Har ik tid lige nu... Tjekker det i morgen.
Avatar billede Slettet bruger
29. november 2005 - 17:18 #8
Hm!
Jeg forstår ingenting...
Hvorfor dur dette ikke?!:

#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <string>
#include <stdlib.h>
using namespace std;

    template <typename T>
bool Get(T &t, istream &is = cin) {
    string s;
    getline(is, s);
    stringstream ss(s);
    return ss >> t;
}
int main() {
    string name;
    int rating;
    string comment;

    cout << "Closing window\n\nHow would you rate C++ from 1 to 5?\nPlease type your ansver and press <ENTER>.\nRating: ";
    Get(rating);

    cout << "\n\nPlease enter a comment and press <ENTER>.\nComment: \n\n";
    getline(cin, comment);

    if(rating < 0) {
        cout << "Whoa! You really don't like C++...\nYou rated it " << rating << "!\nYou also wrote:\n" << comment << "\nInteresting...";
    } else if(rating == 0) {
        cout << "I see you really don't like C++...\nYou rated it " << rating << "!\nYou also wrote:\n" << comment << "\nInteresting...";
    } else if(rating == 1) {
        cout << "I see you  don't like C++...\nYou rated it " << rating << ".\nYou also wrote:\n" << comment << "\nHmm...";
    } else if(rating > 1 && rating < 5) {
        cout << "Ok...\nYou rated it " << rating << "...\nYou also wrote:\n" << comment << "\nPretty normal...";
    } else if(rating == 5) {
        cout << "I see you like C++...\nYou rated it " << rating << "!\nYou also wrote:\n" << comment << "\nI like C++ too!";
    } else if(rating > 5) {
        cout << "I see you really like C++!\nYou rated it " << rating << "!\nYou also wrote:\n" << comment << "\nI like C++ too!";
    } else {
        cout << "There must be some mistake...\nYou rated it " << rating << "!\You also wrote:\n" << comment << "\nWierd...";
    }
    cout "\n\nPress <ENTER> to close this window.";
    cin.get();
}
Avatar billede Slettet bruger
29. november 2005 - 17:20 #9
Hov, et sted står der \ hvor der skulle stå \n... Sorry!
Avatar billede bertelbrander Novice
29. november 2005 - 19:50 #10
Der mangler en << i denne linie: 
  cout "\n\nPress <ENTER> to close this window.";
Avatar billede Slettet bruger
29. november 2005 - 19:51 #11
Ååh... Tester det lige...
Avatar billede Slettet bruger
29. november 2005 - 19:58 #12
Så dur det!
Kode:

#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <string>
#include <stdlib.h>
using namespace std;

    template <typename T>
bool Get(T &t, istream &is = cin) {
    string s;
    getline(is, s);
    stringstream ss(s);
    return ss >> t;
}
int main() {
    string name;
    int rating;
    string comment;

    cout << "Closing window\n\nHow would you rate C++ from 1 to 5?\nPlease type your rating and press <ENTER>.\nRating: ";
    Get(rating);

    cout << "\n\nPlease enter a comment and press <ENTER>.\nComment: \n";
    getline(cin, comment);

    if(rating < 0) {
        cout << "\nWhoa! You really don't like C++...\nYou rated it " << rating << "!\nYou also wrote:\n" << comment << "\nInteresting...";
    } else if(rating == 0) {
        cout << "\nI see you really don't like C++...\nYou rated it " << rating << "!\nYou also wrote:\n" << comment << "\nInteresting...";
    } else if(rating == 1) {
        cout << "\nI see you  don't like C++...\nYou rated it " << rating << ".\nYou also wrote:\n" << comment << "\nHmm...";
    } else if(rating > 1 && rating < 5) {
        cout << "\nOk...\nYou rated it " << rating << "...\nYou also wrote:\n" << comment << "\nPretty normal...";
    } else if(rating == 5) {
        cout << "\nI see you like C++...\nYou rated it " << rating << "!\nYou also wrote:\n" << comment << "\nI like C++ too!";
    } else if(rating > 5) {
        cout << "\nI see you really like C++!\nYou rated it " << rating << "!\nYou also wrote:\n" << comment << "\nI like C++ too!";
    } else {
        cout << "There must be some mistake...\nYou rated it " << rating << "!\nYou also wrote:\n" << comment << "\nWierd...";
    }
    cout << "\n\nPress <ENTER> to close this window.";
    cin.get();
}

Tak for hjælpen begge to!
Jeg kunne bruge begge dele, men pointene går til bertelbrander!
Bare smid et svar...
Avatar billede bertelbrander Novice
29. november 2005 - 20:00 #13
Jeg samler ikke på point.
Avatar billede Slettet bruger
01. december 2005 - 16:36 #14
Fair nok... Tak for hjælpen!
Avatar billede Slettet bruger
01. april 2006 - 08:23 #15
--- LUKKET ---
Avatar billede Slettet bruger
01. april 2006 - 08:24 #16
Oh...
Undskyld, mathiash!
Det gik lige LIDT for stærkt, med at lukke dette spørgsmål!
Avatar billede mathiash Nybegynder
01. april 2006 - 10:43 #17
Gør skam ikke noget, det var bertel's svar der var din løsning ;)
Avatar billede Slettet bruger
01. april 2006 - 18:56 #18
Tak for det...
Jeg hader at lave fejl :S

--- LUKKET - FOR REAL! ---
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester