Thread: why is this not working

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    230

    why is this not working

    Hello

    I have this code
    Code:
    #include <iomanip>
    #include <ios>
    #include <iostream>
    #include <string>
    
    using namespace std ;
    
    int main()
    {
    	// ask for and read the student's name
    	cout << "Please enter your first name: ";
    	string name;
    	cin >> name;
    	cout << "Hello, " << name << "!" << endl;
    
    	// ask for and read the midterm and final grades
    	cout << "Please enter your midterm and final exam grades: ";
    	double midterm, final;
    	cin >> midterm >> final;
    
    	// ask for the homework grades
    	cout << "Enter all your homework grades, "
    	        "followed by end-of-file: ";
    
    	// the number and sum of grades read so far
    	int count = 0;
    	double sum = 0;
    
    	// a variable into which to read
    	double x;
    
    	// invariant:
    	//     we have read `count' grades so far, and
    	//     `sum' is the sum of the first `count' grades
    	while (cin >> x)
    	{
    		++count;
    		sum += x;
    	}
    
        while (count == 0 )
        {
            cout <<"You haven't entered any values" << endl;
            cout << endl ;
            cout << "Enter all your homework grades, "
    	        "followed by end-of-file: ";
            // invariant:
            //     we have read `count' grades so far, and
            //     `sum' is the sum of the first `count' grades
            while (cin >> x)
            {
                ++count;
                sum += x;
            }
        }
    
    	// write the result
    	streamsize prec = cout.precision();
    	cout << "Your final grade is " << setprecision(3)
    	     << 0.2 * midterm + 0.4 * final + 0.4 * sum / count
    	     << setprecision(prec) << endl;
    
    	return 0;
    }
    As the user don't put in any numbers they get a warning and get a chance to do it again.
    But here the warning is displayed without giving the user a moment to do the input again.

    Why is this happening and how can I solve this ?

    Roelof
    Last edited by roelof; 06-19-2010 at 05:11 AM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Most likely because you have crap left in the input buffer. Clear it out with cin.ignore.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    230
    Hoi Elysia.

    Something like this :
    Code:
     while (count == 0 )
        {
            cin.ingnore (1000, '/n') ; 
            cout <<"You haven't entered any values" << endl;
            cout << endl ;
            cout << "Enter all your homework grades, "
    	        "followed by end-of-file: ";
            // invariant:
            //     we have read `count' grades so far, and
            //     `sum' is the sum of the first `count' grades
            while (cin >> x)
            {
                ++count;
                sum += x;
            }
    Roelof

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    You should probably only ignore after something failed ;-). Furthermore, '/n' isn't legal, I think you meant '\n'.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Did you try compiling that?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    230
    Yes.

    I tried it and I mean '\n'

    It compiles but still a infentive loop.
    EVOEx : when count == 0 then it fails because nothing has be inputted.

    Roelof

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Then post your real code, instead of something you typed up full of errors.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    230
    oke,

    Here the code :
    Code:
    #include <iomanip>
    #include <ios>
    #include <iostream>
    #include <string>
    
    using namespace std ;
    
    int main()
    {
    	// ask for and read the student's name
    	cout << "Please enter your first name: ";
    	string name;
    	cin >> name;
    	cout << "Hello, " << name << "!" << endl;
    
    	// ask for and read the midterm and final grades
    	cout << "Please enter your midterm and final exam grades: ";
    	double midterm, final;
    	cin >> midterm >> final;
    
    	// ask for the homework grades
    	cout << "Enter all your homework grades, "
    	        "followed by end-of-file: ";
    
    	// the number and sum of grades read so far
    	int count = 0;
    	double sum = 0;
    
    	// a variable into which to read
    	double x;
    
    	// invariant:
    	//     we have read `count' grades so far, and
    	//     `sum' is the sum of the first `count' grades
    	while (cin >> x)
    	{
    		++count;
    		sum += x;
    	}
    
        while (count == 0 )
        {
            cin.ignore (1000, '\n'); 
            cout <<"You haven't entered any values" << endl;
            cout << endl ;
            cout << "Enter all your homework grades, "
    	        "followed by end-of-file: ";
            // invariant:
            //     we have read `count' grades so far, and
            //     `sum' is the sum of the first `count' grades
            while (cin >> x)
            {
                ++count;
                sum += x;
            }
        }
    
    	// write the result
    	streamsize prec = cout.precision();
    	cout << "Your final grade is " << setprecision(3)
    	     << 0.2 * midterm + 0.4 * final + 0.4 * sum / count
    	     << setprecision(prec) << endl;
    
    	return 0;
    }
    Roelof

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    How about:
    Code:
    #include <iomanip>
    #include <ios>
    #include <iostream>
    #include <string>
    
    using namespace std ;
    
    int main()
    {
    	// ask for and read the student's name
    	cout << "Please enter your first name: ";
    	string name;
    	cin >> name;
    	cout << "Hello, " << name << "!" << endl;
    
    	// ask for and read the midterm and final grades
    	cout << "Please enter your midterm and final exam grades: ";
    	double midterm, final;
    	cin >> midterm >> final;
            cin.ignore (1000); 
    
    	// ask for the homework grades
    	cout << "Enter all your homework grades, "
    	        "followed by end-of-file: ";
    
    	// the number and sum of grades read so far
    	int count = 0;
    	double sum = 0;
    
    	// a variable into which to read
    	double x;
    
    	// invariant:
    	//     we have read `count' grades so far, and
    	//     `sum' is the sum of the first `count' grades
    	while (cin >> x)
    	{
    	        cin.ignore (1000); 
    		++count;
    		sum += x;
    	}
    
        while (count == 0 )
        {
            cin.ignore (1000); 
            cout <<"You haven't entered any values" << endl;
            cout << endl ;
            cout << "Enter all your homework grades, "
    	        "followed by end-of-file: ";
            // invariant:
            //     we have read `count' grades so far, and
            //     `sum' is the sum of the first `count' grades
            while (cin >> x)
            {
    	    cin.ignore (1000); 
                ++count;
                sum += x;
            }
        }
    
    	// write the result
    	streamsize prec = cout.precision();
    	cout << "Your final grade is " << setprecision(3)
    	     << 0.2 * midterm + 0.4 * final + 0.4 * sum / count
    	     << setprecision(prec) << endl;
    
    	return 0;
    }
    Does this work?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    230
    Hoi Elysia,

    It don't work.
    Still only the message that there is no input and that the whole time.

    Roelof

    Edit 1 :

    Maybe use a vector to check for input and after no input use a return 1.
    The same as the example in the book.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Bah. If no one helps you in the meantime, I'll take a look when I get home.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    230
    Oke,

    Don't hurry.
    I think it will be 2020 when I have the book ready.

    Roelof

  13. #13
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    Depending on what you want to do, the logic of the code is broken.

    In this example, I've shown a possible fix and noted part of the problem.

    Soma

    Code:
    #include <iomanip>
    #include <ios>
    #include <iostream>
    #include <string>
    
    using namespace std ;
    
    int main()
    {
    	// ask for and read the student's name
    	cout << "Please enter your first name: ";
    	string name;
    	cin >> name;
    	cout << "Hello, " << name << "!" << endl;
    
    	// ask for and read the midterm and final grades
    	cout << "Please enter your midterm and final exam grades: ";
    	double midterm, final;
    	cin >> midterm >> final;
    
    	// ask for the homework grades
    	cout << "Enter all your homework grades, "
    	        "followed by end-of-file: ";
    
    	// the number and sum of grades read so far
    	int count = 0;
    	double sum = 0;
    
    	// a variable into which to read
    	double x;
    
    	// invariant:
    	//     we have read `count' grades so far, and
    	//     `sum' is the sum of the first `count' grades
    	do {
    	while (cin >> x)
    	{
    		++count;
    		sum += x;
    	}
    	if(!cin.eof())
    	{
          cin.clear();
          cin.ignore(1000, '\n');
          cout << "not a double value\n";
    	}
    	} while(!cin.eof());
    
       // this will not work. the last block runs until EOF
       // EOF is a state that can't logically be ignored
       // or cleared as the next read would just EOF again
        /*while (count == 0 )
        {
            cin.ignore (1000);
            cout <<"You haven't entered any values" << endl;
            cout << endl ;
            cout << "Enter all your homework grades, "
    	        "followed by end-of-file: ";
            // invariant:
            //     we have read `count' grades so far, and
            //     `sum' is the sum of the first `count' grades
            while (cin >> x)
            {
    	    cin.ignore (1000);
                ++count;
                sum += x;
            }
        }*/
    
    	// write the result
    	streamsize prec = cout.precision();
    	cout << "Your final grade is " << setprecision(3)
    	     << 0.2 * midterm + 0.4 * final + 0.4 * sum / count
    	     << setprecision(prec) << endl;
    
    	return 0;
    }

  14. #14
    Registered User
    Join Date
    May 2010
    Posts
    230
    Oke,

    I understand but not complety.
    But why not cin .eof() when displaying the error message
    Must that be cin.eof.
    And if a user enters one value then it's good and the user may use EOF otherwise the loop will run forever.

    Roelof

    Edit 1 : I tried it and it's not working. When you now use a EOF then the programm does not respond anymore
    Last edited by roelof; 06-19-2010 at 09:24 AM.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I got it working with this:
    Code:
    #include <iomanip>
    #include <ios>
    #include <iostream>
    #include <string>
    
    using namespace std ;
    
    int main()
    {
    	// ask for and read the student's name
    	cout << "Please enter your first name: ";
    	string name;
    	cin >> name;
    	cout << "Hello, " << name << "!" << endl;
    
    	// ask for and read the midterm and final grades
    	cout << "Please enter your midterm and final exam grades: ";
    	double midterm, final;
    	cin >> midterm >> final;
    
    	// the number and sum of grades read so far
    	int count = 0;
    	double sum = 0;
    
    	// a variable into which to read
    	double x;
    
    	// invariant:
    	//     we have read `count' grades so far, and
    	//     `sum' is the sum of the first `count' grades
    	while (count == 0 )
    	{
    		// ask for the homework grades
    		cout << "Enter all your homework grades, "
    			"followed by -1: ";
    		for (;;)
    		{
    			cin >> x;
    			if (x == -1)
    				break;
    			++count;
    			sum += x;
    		}
    		if (count == 0)
    		{
    			cout <<"You haven't entered any values" << endl;
    			cout << endl ;
    		}
    	}
    
    	// write the result
    	streamsize prec = cout.precision();
    	cout << "Your final grade is " << setprecision(3)
    		<< 0.2 * midterm + 0.4 * final + 0.4 * sum / count
    		<< setprecision(prec) << endl;
    
    	return 0;
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function not working
    By sloopy in forum C Programming
    Replies: 31
    Last Post: 11-12-2005, 08:08 PM
  2. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  4. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 08:35 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM