CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 3 of 3 FirstFirst 123
Results 31 to 36 of 36
  1. #31
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: why is it skipping lines?

    With all due respect, I find it strange that you are unable to understand while loops and do-while loops.

    I think probably what it comes down to, is that you are in a panic about all you have to do this week, and you are not giving yourself time to stop and think properly about your assignment. "I don't have time to think about it" you might say, but, if you take a deep breath, try to relax and apply your mind to the subject at hand, I would be very surprised if you cannot understand while loops within 60 seconds.

    Once you understand the while loop I would again be very surprised if it takes you more than another 90 seconds (given all the help you've been given on this forum) to understand how to correctly apply it to your program. It should only take you 5 minutes to make the changes to your code. However, even if it takes you 20 minutes (which it really shouldn't), we'd only be asking that you use 22.5 minutes out of your whole week to ammend your a program so that it is correctly designed and implemented. It will get you a better grade, and you will have a better understanding.

    The thing is, while loops are exactly as they read:
    Code:
    while( the condition is true)
    {
      //do whatever is within these curly braces.
    }
    //If we're here then the condition is not true anymore
    For example
    Code:
    bool counted_to_five = false;
    int count = 1;
    
    //remember ! means 'not'
    while(!counted_to_five) //while not counted to five
    {
      //If count is equal to 5
      if(count==5)
      {
        //set counted_to_five to true
        counted_to_five = true;
        //This will mean that !counted_to_five will now resolve to false
        //and the while loop will finish.
      }
      else
      {
        //increment count
        ++count;
      }
    }
    You do not need to understand any more than the above while loop to fix your code. Paul has already given you a suitable solution when he wrote the following pseudo code:

    Quote Originally Posted by Paul McKenzie
    Code:
    start program
    
    set selection to 'a'
    
    while selection is not 'q'
    {
        display menu and get selection
        if ( selection is 'a') do a stuff
        if ( selection is 'b') do b stuff
    }  
    
    end program
    Quote Originally Posted by CyberShot
    The program doesn't get graded on being flawless.
    I don't think you are in a position to judge on that. I've had a fair amount of experience of marking University Students work (though mainly for physics rather than programming). When I've done marking I've always judged against a flawless answer (and we were encouraged to do so by the head of department). The point is, if the lecturer or marker doesn't do that, then they have no reference point for how to grade the answers, everything becomes relative or subjective and students often get dealt unfair marks - those who get marked too high don't push themselves to learn as they should, and those who get marked too low often become despondent with their results never really understanding why their mark is so low. The only fair thing to do in subject where there are concrete right and wrong anwers it to mark against a 'flawless' answer.

    In programming terms I would expect your program to be logically correct - which it isn't at present. The opinion of most here is that your program will get marked down if you leave it as it is. We're trying to help you, and we're not asking you to spend hours on it, but just a number of dedicated minutes instead.

  2. #32
    Join Date
    Jan 2008
    Posts
    43

    Re: why is it skipping lines?

    Thank you guys

  3. #33
    Join Date
    Jan 2008
    Posts
    43

    Re: why is it skipping lines?

    Okay Paul, I re worked the program. Is this what you meant by doing it correctly?

    Code:
    // IntegrityCheck.cpp : main project file.
    
    #include "stdafx.h"
    #include <string>
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    #include <fstream>
    using namespace std;
    
    void addChecksum();
    void runChecksum();
    
    int main()
    {
       	char choice;
    	bool finished = false;
    
    	while(!finished)
    	{
    		cout << "Please Select: " << endl;
    		cout << "  A) Add checksum to specified file: " << endl;
    		cout << "  B) Verify integrity of specified file" << endl;
    		cout << "  Q) Quit" << endl;
    		cin >> choice;
    	
    		switch (choice)
    		{
    			case 'A': 
    				addChecksum();
    				break;
    			case 'a':
    				addChecksum();
    				break;
    			case 'B':
    				runChecksum();
    				break;
    			case 'b':
    				runChecksum();
    				break;
    			case 'Q':
    				finished = true;
    				break;
    			case 'q':
    				finished = true;
    				break;
    
    			default:
    				cout << "Your choice did not match any of the menu options." << endl;
    		}
    	
    	}
    	
    	system("pause");
        return 0;
    }
    
    void addChecksum()
    {
    	string inputFileName;
    	ifstream inputFile;
    	string line;
    	
    	cout << "Specify the file path: ";
    	cin.ignore();
    	getline(cin, inputFileName);
    	inputFile.open(inputFileName.c_str());
    	cout << endl;
    	
    	inputFile.clear();
    	
    	while(!inputFile.is_open())
    	{
    		cout << "The file " << inputFileName << " could not be found or opened!" << endl;
    		cout << endl;
    		return;
    	}
    	
    	cout << endl;
    	
    	//This will output the text to the screen. I did this for testing purposes
    	//while(inputFile.peek() != EOF)
    	//{
    	//	getline(inputFile, line);
    	//	cout << line << endl;
    	//}
    
    }
    
    void runChecksum()
    {
    	string inputFileName;
    	ifstream inputFile;
    	string lines;
    	int checksum;
    
    	checksum = 0;
    
    	while(!inputFile.is_open())
    	{
    		cout << "Specify the file path: " << inputFileName;
    		getline(cin, inputFileName);
    
    		inputFile.open(inputFileName.c_str());
    		//trying to convert the inputFile into a number
    		//checksum = static_cast<int>(inputFile);
    	}
    
    	inputFile.clear();
    	cout << "File checksum = " << checksum << endl;
    }

  4. #34
    Join Date
    Aug 2007
    Posts
    858

    Re: why is it skipping lines?

    I didn't read the code in detail, but yes that looks more like what we're talking about.

    One thing though, on switch statements you can stack multiple options like so:

    Code:
    switch(choice)
    {
    	case 'A':
    	case 'a':
    		addCheckSum();
    		break;
    		
    	case 'B':
    	case 'b':
    		runChecksum();
    		break;
    	
    	// etc
    }
    What you have now works fine, it's just easier and cleaner to avoid the redundant code.

  5. #35
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: why is it skipping lines?

    Quote Originally Posted by CyberShot View Post
    Code:
            //trying to convert the inputFile into a number
            //checksum = static_cast<int>(inputFile);
    Just to add another one:

    This is definitely not the way to calculate a checksum. A checksum is supposed to indicate that a file has not changed, so it has to depend on the contents of the file. What you have there at best depends on how your C++ compiler/runtime handles the file stream object and may (or may not) change at will, regardless of whether the file has changed or not.

    And in addition I doubt that this static cast would compile at all.

    But you may want to think about that later, once you got the rest of your program working correctly.

  6. #36
    Join Date
    Apr 1999
    Posts
    27,449

    Re: why is it skipping lines?

    Quote Originally Posted by CyberShot View Post
    Okay Paul, I re worked the program. Is this what you meant by doing it correctly?
    Yes, with the other observations made by Speedo.

    Regards,

    Paul McKenzie

Page 3 of 3 FirstFirst 123

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