954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

bit of help with please :)

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;
    
    
}
slawted
Newbie Poster
19 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

post your compile errors

monkey_king
Junior Poster
160 posts since Aug 2008
Reputation Points: 70
Solved Threads: 9
 

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

slawted
Newbie Poster
19 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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;
    
   
}
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

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

slawted
Newbie Poster
19 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 
WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 
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

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

thank you for your support guys

slawted
Newbie Poster
19 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 
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 :)

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

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;
    
   
}
slawted
Newbie Poster
19 posts since Jun 2009
Reputation Points: 10
Solved Threads: 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

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

>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 :(

siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140
 

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 .

slawted
Newbie Poster
19 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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.

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

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.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You