hello guys i recieve errors when compiling my code

#include <iostream>
#include <fstream>
#include <string>

int main() {
    
std::string firstname, lastname, age, emailadress, message;
char answer;
    
    std::cout << "Hello please imput your first name: ";
    std::getline(std::cin, firstname);
    std::cout << "\nNow please enter your last name: ";
    std::getline(std::cin, lastname);
    std::cout << "\nYour age: ";
    std::getline(std::cin, age);
    std::cout << "\nContact email: ";
    std::getline(std::cin, emailadress);
    std::cout << "\nNow finally what is your message: ";
    std::getline(std::cin, message);
    std::cout << "\nPress Enter to continue.\n\n";  
    std::cin.ignore(255,'\n'); 
    std::cout << "The following infomation has been recieved...\n";
    std::cout << "Name: " << firstname << " " << lastname << "\nAge: " << age << "\nEmail Adress: ";
    std::cout << emailadress << "\nMessage: " << message; 
    std::cout << "\n\nPress Enter to continue.\n\n";
    std::cin.ignore(255,'\n');
    std::cout << "Would you like to subit your message: [Y/N] ";
    switch (answer) {
           case 'Y':
           case 'y':
std::ofstream fileOutput("Customer support.txt", std::ios::app);
if (fileOutput.is_open()) {
                              
fileOutput << "Name: " << firstname << " " << lastname << "\nAge: " << age << "\nEmail Adress: " << emailadress << "\nMessage: " << message;
                              
                              fileOutput.close();
std::cout <<"\n\nThank you for your submission, your infomation has been saved. We will contact you shortly.\n";
          break;         
        case 'N':
          case 'n':
std:: cout <<"Thank you anyway."; 

break;
default:
        std::cout <<"Please choose an option, would you like to submit your message: [Y/N]";
        }
    std::cin.ignore(255,'\n'); 
    return 0;
    
    
}

Recommended Answers

All 15 Replies

post your compile errors

sure :)

Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\Users\Michael\Desktop\C++\FIRST REAL.cpp" -o "C:\Users\Michael\Desktop\C++\FIRST REAL.exe" -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
C:\Users\Michael\Desktop\C++\FIRST REAL.cpp: In function `int main()':
C:\Users\Michael\Desktop\C++\FIRST REAL.cpp:39: error: jump to case label
C:\Users\Michael\Desktop\C++\FIRST REAL.cpp:31: error: crosses initialization of `std::ofstream fileOutput'
C:\Users\Michael\Desktop\C++\FIRST REAL.cpp:40: error: jump to case label
C:\Users\Michael\Desktop\C++\FIRST REAL.cpp:31: error: crosses initialization of `std::ofstream fileOutput'
C:\Users\Michael\Desktop\C++\FIRST REAL.cpp:44: error: jump to case label
C:\Users\Michael\Desktop\C++\FIRST REAL.cpp:31: error: crosses initialization of `std::ofstream fileOutput'
C:\Users\Michael\Desktop\C++\FIRST REAL.cpp:39: warning: destructor needed for `fileOutput'
C:\Users\Michael\Desktop\C++\FIRST REAL.cpp:39: warning: where case label appears here
C:\Users\Michael\Desktop\C++\FIRST REAL.cpp:39: warning: (enclose actions of previous case statements requiring destructors in their own scope.)
C:\Users\Michael\Desktop\C++\FIRST REAL.cpp:40: warning: destructor needed for `fileOutput'
C:\Users\Michael\Desktop\C++\FIRST REAL.cpp:40: warning: where case label appears here
C:\Users\Michael\Desktop\C++\FIRST REAL.cpp:44: warning: destructor needed for `fileOutput'
C:\Users\Michael\Desktop\C++\FIRST REAL.cpp:44: warning: where case label appears here

Execution terminated

You have a brackets problem. You need to indent your code so that blocks of code line up. Otherwise you can't see brackets problems. You have an if statement (line 32) with a starting bracket but no ending bracket. Indent the code properly and the lack of bracket will really stick out. You can't see it when the code is indented as you have indented it.

#include <iostream>
#include <fstream>
#include <string>

int main() {
    
std::string firstname, lastname, age, emailadress, message;
char answer;
    
    std::cout << "Hello please imput your first name: ";
    std::getline(std::cin, firstname);
    std::cout << "\nNow please enter your last name: ";
    std::getline(std::cin, lastname);
    std::cout << "\nYour age: ";
    std::getline(std::cin, age);
    std::cout << "\nContact email: ";
    std::getline(std::cin, emailadress);
    std::cout << "\nNow finally what is your message: ";
    std::getline(std::cin, message);
    std::cout << "\nPress Enter to continue.\n\n";  
    std::cin.ignore(255,'\n'); 
    std::cout << "The following infomation has been recieved...\n";
    std::cout << "Name: " << firstname << " " << lastname << "\nAge: " << age << "\nEmail Adress: ";
    std::cout << emailadress << "\nMessage: " << message; 
    std::cout << "\n\nPress Enter to continue.\n\n";
    std::cin.ignore(255,'\n');
    std::cout << "Would you like to subit your message: [Y/N] ";
    switch (answer) {
           case 'Y':
           case 'y':
std::ofstream fileOutput("Customer support.txt", std::ios::app);
if (fileOutput.is_open()) {
                              
fileOutput << "Name: " << firstname << " " << lastname << "\nAge: " << age << "\nEmail Adress: " << emailadress << "\nMessage: " << message;
                              
                              fileOutput.close();
std::cout <<"\n\nThank you for your submission, your infomation has been saved. We will contact you shortly.\n";
          break;         
        case 'N':
          case 'n':
std:: cout <<"Thank you anyway."; 

break;
default:
        std::cout <<"Please choose an option, would you like to submit your message: [Y/N]";
        }
    std::cin.ignore(255,'\n'); 
    return 0;
    
   
}

do you mean a { or a ( ? cause i still says the case 'N' is wrong :S

I mean a }. You have a { on line 32, with no matching }. There could be other problems too, but that's definitely ONE problem.

See this for formatting help

Or use AStyle for code formatting :P

To the OP (please don't take this personal): What do you want? A bit of help or a byte of help? :P

thank you for your support guys

Or use AStyle for code formatting :P

Or use Visual Studio and press CTRL-A, CTRL-K, CTRL-F.
Or if linux is your taste: Use Netbeans and click source->format.

I'm sure most IDE's have this kind of functionality build in :)

im currently in school and cant compile does anybody know if this would compile?

#include <iostream>
#include <fstream>
#include <string>

int main() {
    
std::string firstname, lastname, age, emailadress, message;
char answer;
    
    std::cout << "Hello please imput your first name: ";
    std::getline(std::cin, firstname);
    std::cout << "\nNow please enter your last name: ";
    std::getline(std::cin, lastname);
    std::cout << "\nYour age: ";
    std::getline(std::cin, age);
    std::cout << "\nContact email: ";
    std::getline(std::cin, emailadress);
    std::cout << "\nNow finally what is your message: ";
    std::getline(std::cin, message);
    std::cout << "\nPress Enter to continue.\n\n";  
    std::cin.ignore(255,'\n'); 
    std::cout << "The following infomation has been recieved...\n";
    std::cout << "Name: " << firstname << " " << lastname << "\nAge: " << age << "\nEmail Adress: ";
    std::cout << emailadress << "\nMessage: " << message; 
    std::cout << "\n\nPress Enter to continue.\n\n";
    std::cin.ignore(255,'\n');
    std::cout << "Would you like to subit your message: [Y/N] ";
    switch (answer) {
           case 'Y':
           case 'y':
std::ofstream fileOutput("Customer support.txt", std::ios::app);
if (fileOutput.is_open()) {
                              
fileOutput << "Name: " << firstname << " " << lastname << "\nAge: " << age << "\nEmail Adress: " << emailadress << "\nMessage: " << message;
                              
                              fileOutput.close();}
std::cout <<"\n\nThank you for your submission, your infomation has been saved. We will contact you shortly.\n";
          break;         
        case 'N':
          case 'n':
std:: cout <<"Thank you anyway."; 

break;
default:
        std::cout <<"Please choose an option, would you like to submit your message: [Y/N]";
        }
    std::cin.ignore(255,'\n'); 
    return 0;
    
   
}

hey guys im currently in school so i wont be able to compile, does anybody know if this would compile?

Yes, I know, I just ran it through my compiler, result: no it doesn't compile, here are the errors, so that you know where to make adjustments in your code:

ttt.cpp: In function `int main()':
ttt.cpp:40: error: jump to case label
ttt.cpp:31: error:   crosses initialization of `std::ofstream fileOutput'
ttt.cpp:41: error: jump to case label
ttt.cpp:31: error:   crosses initialization of `std::ofstream fileOutput'
ttt.cpp:45: error: jump to case label
ttt.cpp:31: error:   crosses initialization of `std::ofstream fileOutput'
ttt.cpp:40: warning: destructor needed for `fileOutput'
ttt.cpp:40: warning: where case label appears here
ttt.cpp:40: warning: (enclose actions of previous case statements requiring dest
ructors in their own scope.)
ttt.cpp:41: warning: destructor needed for `fileOutput'
ttt.cpp:41: warning: where case label appears here
ttt.cpp:45: warning: destructor needed for `fileOutput'
ttt.cpp:45: warning: where case label appears here
ttt.cpp:52:2: warning: no newline at end of file

:P

P.S: Please bear in mind that Daniweb is no remote compiler service or something :P

>Please bear in mind that Daniweb is no remote compiler service or something
What a good idea. I have started to think to write one :-)
Bloody who will support the webhosting services :(

commented: You are really unbelievable ! First you write an excellent guide, now siddhant is designing a remote compiler service :P +9
commented: Nice idea, but it already exists... +20

thank you for your support guys i really appriciate it, although im a new learner of c++ and cannot spot were to put my }.
Im sorry if im doing your head in guy :(

thanks .

thank you for your support guys i really appriciate it, although im a new learner of c++ and cannot spot were to put my }.
Im sorry if im doing your head in guy :(

thanks .

Formatting! Makes it much easier to read. You'll see immediately that the brackets mismatch isn't there anymore.

#include <iostream>
#include <fstream>
#include <string>

int main() 
{
    std::string firstname, lastname, age, emailadress, message;
    char answer;

    std::cout << "Hello please imput your first name: ";
    std::getline(std::cin, firstname);
    std::cout << "\nNow please enter your last name: ";
    std::getline(std::cin, lastname);
    std::cout << "\nYour age: ";
    std::getline(std::cin, age);
    std::cout << "\nContact email: ";
    std::getline(std::cin, emailadress);
    std::cout << "\nNow finally what is your message: ";
    std::getline(std::cin, message);
    std::cout << "\nPress Enter to continue.\n\n";
    std::cin.ignore(255, '\n');
    std::cout << "The following infomation has been recieved...\n";
    std::cout << "Name: " << firstname << " " << lastname << "\nAge: " << age << "\nEmail Adress: ";
    std::cout << emailadress << "\nMessage: " << message;
    std::cout << "\n\nPress Enter to continue.\n\n";
    std::cin.ignore(255, '\n');
    std::cout << "Would you like to subit your message: [Y/N] ";
    switch (answer) 
    {
        case 'Y':
        case 'y':
            std::ofstream fileOutput("Customer support.txt", std::ios::app);
            if (fileOutput.is_open()) 
            {
                fileOutput << "Name: " << firstname << " " << lastname << "\nAge: " << age <<
                        "\nEmail Adress: " << emailadress << "\nMessage: " << message;
                fileOutput.close();
            }
            std::cout << "\n\nThank you for your submission, your infomation has been saved. We will contact you shortly.\n";
            break;
        case 'N':
        case 'n':
            std::cout << "Thank you anyway.";
            break;
        default:
            std::cout << "Please choose an option, would you like to submit your message: [Y/N]";
    }
    std::cin.ignore(255, '\n');
    return 0;
}

But you still have a brackets problem. It's just not a mismatch problem like it used to be. Try this:

#include <iostream>
#include <fstream>
#include <string>

int main() 
{
    std::string firstname, lastname, age, emailadress, message;
    char answer;

    std::cout << "Hello please imput your first name: ";
    std::getline(std::cin, firstname);
    std::cout << "\nNow please enter your last name: ";
    std::getline(std::cin, lastname);
    std::cout << "\nYour age: ";
    std::getline(std::cin, age);
    std::cout << "\nContact email: ";
    std::getline(std::cin, emailadress);
    std::cout << "\nNow finally what is your message: ";
    std::getline(std::cin, message);
    std::cout << "\nPress Enter to continue.\n\n";
    std::cin.ignore(255, '\n');
    std::cout << "The following infomation has been recieved...\n";
    std::cout << "Name: " << firstname << " " << lastname << "\nAge: " << age << "\nEmail Adress: ";
    std::cout << emailadress << "\nMessage: " << message;
    std::cout << "\n\nPress Enter to continue.\n\n";
    std::cin.ignore(255, '\n');
    std::cout << "Would you like to subit your message: [Y/N] ";
    switch (answer) 
    {
        case 'Y':
        case 'y':
            {
                std::ofstream fileOutput("Customer support.txt", std::ios::app);
                if (fileOutput.is_open()) 
                {
                    fileOutput << "Name: " << firstname << " " << lastname << "\nAge: " << age <<
                            "\nEmail Adress: " << emailadress << "\nMessage: " << message;
                    fileOutput.close();
                }
                std::cout << "\n\nThank you for your submission, your infomation has been saved. We will contact you shortly.\n";
            }
            break;
        case 'N':
        case 'n':
            std::cout << "Thank you anyway.";
            break;
        default:
            std::cout << "Please choose an option, would you like to submit your message: [Y/N]";
    }
    std::cin.ignore(255, '\n');
    return 0;
}

See the new brackets on lines 32 and 41.

For future reference, for (technical) reasons I've forgotten, if I ever knew them, you need to enclose the body of a case statement in curly brackets if you declare a new variable within the body of the statement. Or maybe it's like using curly brackets with single line bodies of control loops and if/else statements, you don't have to use curly brackets in case statements if you don't declare new variables in the statement. Whatever........ I suspect that someone can provide the definitive ruling govening this protocol and a rationale for why it is the way it is, but if you want to avoid the hassle again in the future, now you know what to do and why (to a first apporoximation anyway) VernonDozier did what he did.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.