CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Dec 2005
    Posts
    3

    Exclamation What's Wrong With My Code?

    Here's my Code. I don't know why it doesn't work. I've tried a couple different IDEs. (Turbo C++, Dev C++, and CodeBlocks)
    ------------------------------------------------------------------------------------------------

    // Created By Bobby Goulet
    // Last Updated on December 10th 2005
    // Page 366 #21 // This program will print a list of all
    // positive integers less than 100 that
    // are divisible by either 5 or 7. It
    // will then print the number of
    // integers that were found.
    // Include Files
    # include<iostream>
    # include<iomanip>
    # include<conio.h>
    # include<apstring.cpp>

    // Program
    int main()
    {
    // Formatting
    clrscr();
    cout<<setiosflags(ios::left|ios::fixed|ios::showpoint);
    cout<<setprecision(2);
    // Variables
    string Start;
    int Stop;
    int Number;
    int ARemainder;
    int BRemainder;
    int Total;
    // Introductory Text
    cout<<"This program will print a list of all positive integers less than ";
    cout<<"100 that are divisible by either 5 or 7. It will then print the ";
    cout<<"number of integers that were found. To start the program, type in ";
    cout<<"\"GO\"";
    // Program Start Loop
    cin>>Start;
    while(Start!="GO"||Start!="go"||Start!="Go")
    {
    cin>>Start;
    Stop=Stop+1;
    if(Stop==5)
    {
    cout<<"(Type STOP to exit)";
    Stop=0;
    }
    if(Start=="STOP"||Start=="STOP"||Start=="stop")
    {
    getch();
    }
    }
    // Calculations
    for(int Number=1; Number<=100 ; Number++)
    {
    ARemainder=Number%5;
    if(ARemainder==0)
    {
    cout<<Number<<" is divisible by 5.";
    Total=Total+1;
    }
    BRemainder=Number%7;
    if(BRemainder==0)
    {
    cout<<Number<<" is divisible by 7.";
    Total=Total+1;
    }
    if(ARemainder==0&&BRemainder==0)
    {
    Total=Total-1;
    }
    }
    // Results
    cout<<"A total of "<<Total<<" numbers were divisible by either 5 or 7.";
    getch();
    return 0;
    }
    -------------------------------------------------------------------------------------

    There always seems to be a problem with my APSTRING functions.
    That might be part of the problem.

  2. #2
    Join Date
    Jun 2002
    Posts
    1,417

    Re: What's Wrong With My Code?

    The first thing wrong is that you didn't use code tags to retain spacing and tabs

    2. what do you mean "it doesn't work"? you need to provide a better description of the problem. do you take your car to auto mechanic and just say "it doesn't work"?

  3. #3
    Join Date
    Dec 2005
    Posts
    3

    Lightbulb Re: What's Wrong With My Code?

    Most of the problem seems to be that none of my IDEs recognize APSTRING.CPP. And the compilers say that functions like "cout" and "ios" haven't been declared. how do you add APSTRING.CPP into your runtime library and have it work?

  4. #4
    Join Date
    May 2004
    Posts
    28

    Re: What's Wrong With My Code?

    Here, this should do the trick. And btw, that appstring.cpp wasn't even needed.

    Code:
    #include <iostream.h>
    #include <string>
    #include <conio.h>
    #include <iomanip.h>
    
    using namespace std;
    
    // Program
    int main()
    {
    	// Formatting
    	cout<<setiosflags(ios::left|ios::fixed|ios::showpoint);
    	cout<<setprecision(2);
    	
    	// Variables
    	char Start[4];
    	int Stop = 0;
    	int Number = 0;
    	int ARemainder;
    	int BRemainder;
    	int Total = 0;
    
    	// Introductory Text
    	cout<<"This program will print a list of all positive integers less than ";
    	cout<<"100 that are divisible by either 5 or 7. It will then print the ";
    	cout<<"number of integers that were found. To start the program, type in ";
    	cout<<"\"GO\"" << endl;
    
    	// Program Start Loop
    
    	cin >> Start;
    	while(strcmp(Start,"GO") != 0)
    	{
    		cin >> Start;
    		Stop=Stop+1;
    		if(Stop==5)
    		{
    			cout<<"(Type STOP to exit)" << endl;
    			Stop=0;
    		}
    		if(strcmp(Start,"STOP") == 0)
    		return 0;
    	}
    
    	// Calculations
    	for(Number=1; Number<=100 ; Number++)
    	{
    		ARemainder=Number%5;
    		if(ARemainder==0)
    		{
    			cout<<Number<<" is divisible by 5." << endl;
    			Total=Total+1;
    		}
    
    		BRemainder=Number%7;
    		if(BRemainder==0)
    		{
    			cout<<Number<<" is divisible by 7." << endl ;
    			Total=Total+1;
    		}
    		if(ARemainder==0 && BRemainder==0)
    		{
    			Total=Total-1;
    		}
    	}
    	
    	// Results
    	cout<<"A total of "<<Total<<" numbers were divisible by either 5 or 7." << endl;
    	return 0;
    }

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: What's Wrong With My Code?

    Quote Originally Posted by Xatrix
    Here, this should do the trick. And btw, that appstring.cpp wasn't even needed.

    Code:
    #include <iostream.h>
    #include <string>
    #include <conio.h>
    #include <iomanip.h>
    Wrong headers. Correction:
    Code:
    #include <iostream>
    #include <string>
    #include <iomanip>
    //...
    using namespace std;
    Regards,

    Paul McKenzie

  6. #6
    Join Date
    May 2004
    Posts
    28

    Re: What's Wrong With My Code?

    that's the point, guess they differ somehow, but not wrong. Yours (headers) won't and don't work with my compiler (using VC++ v6.0 SP6), in fact, I've pasted the working code. If you know the reason "your" version of hearders don't work for me, please let me know, I'd like to know that, cause that has bugged me for awhile...

    Oleg

  7. #7
    Join Date
    Dec 2005
    Posts
    642

    Re: What's Wrong With My Code?

    Quote Originally Posted by Xatrix
    that's the point, guess they differ somehow, but not wrong. Yours (headers) won't and don't work with my compiler (using VC++ v6.0 SP6), in fact, I've pasted the working code. If you know the reason "your" version of hearders don't work for me, please let me know, I'd like to know that, cause that has bugged me for awhile...

    Oleg
    Back in the Good Old Days, iostream wasn't part of STL. So it had its own header files iostream.h, iomanip.h, etc. with type names in global scope, not in the std scope. During some period, MS had two versions of iostream, the old form in the include files that end with .h, and the std form in include files without the .h. I think VC++ 6.0 had both versions, although it might not have had the std version. Anyway, starting with VC++ 7.0 or 7.1 (not sure) they phased out the old version of iostream and left the std-only version which uses the include files without the .h.

  8. #8
    Join Date
    Dec 2005
    Posts
    3

    Re: What's Wrong With My Code?

    Hey, thanks, guys.

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: What's Wrong With My Code?

    Quote Originally Posted by Xatrix
    that's the point, guess they differ somehow, but not wrong.
    Since this is the non-Visual C++ forum, those headers are wrong (they're even wrong for Visual C++ 7.2 and above).

    First, those".h" headers are non-standard headers. They should not be used for today's C++ programs. The headers that I posted (the ones without the ".h") are the standard ANSI C++ headers and are to be used over the non-standard headers. Every modern ANSI C++ compiler must support those standard headers.

    Second, the latest version of Visual C++ does not have those ".h" headers. They have been eliminated, and only the standard headers exist. Therefore your program will not compile at all with Visual Studio 2003 and above.

    Third, your program uses std::string. From what I remember, the nonstandard Visual C++ 6.0 version of <iostream.h> did not overload operator >> and << for std::string. The standard <iostream> contains the proper overload of >> and << for std::string.

    Fourth, if you ever get a problem with the input/output streaming functions, STL functions, etc. and you post ".h" version of the standard headers, then there is no guarantee that those ".h" headers work as they are supposed to according to the ANSI C++ standard. The standard headers are guaranteed to behave as described by ANSI C++.

    Fifth, Visual C++ 6.0 does have those standard headers, so either you didn't install the compiler correctly, or you have some other problem.

    Regards,

    Paul McKenzie

  10. #10
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: What's Wrong With My Code?

    Keep Start as type string though. Do not make it a char* or char[4] (not big enough to hold Stop anyway and will cause a memory error if the user enters any string more than 3 characters).

    By the way, why do you need the user to input anything in order to output numbers from 1 to 100 divisible by 5 or 7?

    It may surprise many but I can't remember the last time I actually used cin. Most of my apps are config-based so any user input comes from a config file and not run-time input.

    You might want to end the early cout statements with a newline ('\n')

  11. #11
    Join Date
    Jun 2002
    Posts
    1,417

    Re: What's Wrong With My Code?

    Quote Originally Posted by Xatrix
    that's the point, guess they differ somehow, but not wrong. Yours (headers) won't and don't work with my compiler (using VC++ v6.0 SP6),
    funny -- I use the same compiler and have no problems using the headers without the .h extension.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured