Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > C/C++
You last visited: Today at 09:02

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



C++ Help

Discussion on C++ Help within the C/C++ forum part of the Coders Den category.

Reply
 
Old   #1
 
elite*gold: 0
Join Date: Jun 2014
Posts: 223
Received Thanks: 43
C++ Help

Guten Tag,

Ich lerne gerade C++ wo ist der fehler ?
PHP Code:
#include <iostream>
#include <windows.h>
#include <tchar.h>
#include <stdlib.h>

using namespace std;
char abfrage;

int main (void)
{
    
SetConsoleTitle(_T("Abfrage"));
    
    
cout << "Willkommen im Lernen Fenster"
         
<< endl
         
<< "Heute lernen wir C++"
         
<< endl
         
<< "Guten Tag, sind sie 21 Jahre Jung ? Ja (j) oder Nein (n)"

    
cin >> abfrage;  
    
    if (
abfrage == 'j');
    {
        
cout << "Ich habe es gewusst..." << endl;
    }
    
    else if (
abfrage == 'n');
    {
        
cout << "Schade, ich dachte Sie weahren 21"
             
<< "Können Sie mir ihr alter verratten ? Ja (j) oder Nein (n)";
    }
    
   
    
cin.sync();
cin.get();
return 
0;

Fehler:
PHP Code:
else without a prevoius if 
Prof.MindFreak is offline  
Old 09/09/2014, 20:10   #2
 
elite*gold: 198
Join Date: Mar 2011
Posts: 835
Received Thanks: 263
No ";" behind
Quote:
Code:
if (abfrage == 'j');
and

Quote:
Code:
else if (abfrage == 'n');
This should work:
Code:
#include <windows.h>
#include <iostream> // For cin, cout and endl
#include <tchar.h> 
#include <stdlib.h> 

using namespace std; 
char abfrage; 

int main (void) 
{ 
    SetConsoleTitle(_T("Abfrage")); 
     
    cout << "Willkommen im Lernen Fenster" 
         << endl
         << "Heute lernen wir C++" 
         << endl
         << "Guten Tag, sind sie 21 Jahre Jung ? Ja (j) oder Nein (n)";  

    cin >> abfrage;   
     
    if (abfrage == 'j') // no ";" here with a ";" it wont run the code in the brackets
    { 
        cout << "Ich habe es gewusst..." << endl;
    } 
     
    else if (abfrage == 'n') // no ";" here with a ";" it wont run the code in the brackets
    { 
        cout << "Schade, ich dachte Sie weahren 21"
             << endl // i added this so it looks betther in the console (text was to long)
             << "Können Sie mir ihr alter verratten ? Ja (j) oder Nein (n)"; 
    } 
     
    
     
cin.sync(); 
cin.get(); 
return 0; 
}
ƬheGame is offline  
Thanks
1 User
Old 09/09/2014, 20:10   #3
 
Padmak's Avatar
 
elite*gold: 58
Join Date: Jun 2008
Posts: 2,311
Received Thanks: 8,420
Dass du ein else hast, ohne vorher ein if zu haben.
Spaß beiseite, schmeiß die ; hinter if und else if raus

€: ****, zu langsam. iostream ist aber schon included

Padmak
Padmak is offline  
Thanks
2 Users
Old 09/09/2014, 20:14   #4
 
elite*gold: 198
Join Date: Mar 2011
Posts: 835
Received Thanks: 263
Quote:
Originally Posted by Padmak View Post
Dass du ein else hast, ohne vorher ein if zu haben.
Spaß beiseite, schmeiß die ; hinter if und else if raus

€: ****, zu langsam. iostream ist aber schon included

Padmak
Copy pasta fail

Thanks padmak
ƬheGame is offline  
Old 09/09/2014, 20:16   #5
 
elite*gold: 0
Join Date: Jun 2014
Posts: 223
Received Thanks: 43
Danke wieder was gelernt

Für was ist das #include <windows.h>
und das #include <tchar.h>
Prof.MindFreak is offline  
Old 09/10/2014, 07:53   #6

 
elite*gold: 0
Join Date: Feb 2008
Posts: 2,754
Received Thanks: 1,748
Quote:
Originally Posted by Prof.MindFreak View Post
Danke wieder was gelernt

Für was ist das #include <windows.h>
und das #include <tchar.h>
Für folgende Zeile
Code:
    SetConsoleTitle(_T("Abfrage"));

TCHAR ist fürs Encoding. Es gibt in der WinAPI z.B. SetConsoleTitleA für ANSI Codierte Strings und SetConsoleTitleW für Unicodestrings.
Damit du nicht immer selbst entscheiden musst, welches Encoding du für welchen String möchtest gibt es hierfür _T welches dir den Projekteinstellungen entsprechend die Kodierung auswählt. So kannst du einfach SetConsoleTitle verwenden (welches dann intern entsprechend SetConsoleTitleW oder SetConsoleTitleA aufruft) ohne das ganze explizit benennen zu müssen.

Die windows.h brauchst für die die SetConsoleTitle API, welche windowsspezifisch ist.

TL;DR:
tchar ist zur Kodierungskompatibilität und windows damit du SetConsoleTitle benutzen kannst.
Computerfreek is offline  
Thanks
1 User
Reply




All times are GMT +1. The time now is 09:03.


Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2026 elitepvpers All Rights Reserved.