Thread: Program flow

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    932

    Program flow

    Would there be a simpler way to do this?
    Code:
                           
    int i=0 , j=0;
      while (i < ((int)words2.size()))
      {
         {if (words1[j] == words2[i])
         
                cout << words1[j] <<" is the same as " << words2[i]<< "\n";
         
         else
                cout << words1[j] <<" is not the same as " << words2[i]<< "\n"; 
         }i++;
      }  
      i = 0; j++; 
       while (i < ((int)words2.size()))
      {
         {if (words1[j] == words2[i])
         
                cout << words1[j] <<" is the same as " << words2[i]<< "\n";
         
         else
                cout << words1[j] <<" is not the same as " << words2[i]<< "\n"; 
         }i++;
      }
    Thanks!

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    I thought about this, but it isnt working
    Code:
                           
     int i=0 , j=0;
      while (j < ((int)words1.size()))
      {
       while (i < ((int)words2.size()))
       {
          {if (words1[j] == words2[i])
         
                 cout << words1[j] <<" is the same as " << words2[i]<< "\n";
          
          else
                 cout << words1[j] <<" is not the same as " << words2[i]<< "\n";
          }i++;
       }i=0 ; j++;
      }

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    	int i = 0, j = 0;
    	while ( i < (int)words2.size() )
    	{
    		if (words1[j] == words2[i])
    			cout << words1[j] <<" is the same as " << words2[i]<< "\n";
    		else
    			cout << words1[j] <<" is not the same as " << words2[i]<< "\n"; 
    		i++;
    	}  
    	i = 0;
    	j++; 
    	while ( i < (int)words2.size() )
    	{
    		if (words1[j] == words2[i])
    			cout << words1[j] <<" is the same as " << words2[i]<< "\n";
    		else
    			cout << words1[j] <<" is not the same as " << words2[i]<< "\n"; 
    		i++;
    	}
    Please, indent correctly. Your code is confusing and I had to clean it up to make it readable.
    This doesn't make that much sense. I'm thinking you're trying to compare lots of different words, like words1, words2, words3, etc?
    Those while loops are basically for loops to, so you can change 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.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I suppose the question is: what are you trying to achieve? Are you trying to compare words, or letters, or every element of words1 to words2, or what?

    (PS: Doesn't .size() return an int by itself? Why the cast?)

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Ok thanks, im trying to find the right way to indent, as i didnt like too much the traditional way.
    But i guess i will have to stick with it as i cant seem to find better.
    Ok i try to post the whole code indented properly.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    So, again, what is the code trying to do?
    Btw, I can give you some tips in indentation here:

    Code:
    {if (words1[j] == words2[i])
    This is confusing because you're actually making a local block and putting the start at the same line as the if. Break it into two lines. Typically it's much easier to read if all statements take one line each. Don't put two statements or lines into one.

    Code:
         }i++;
    End of local block, i + 1 (basically for loop), again confusing due to two lines combined into one.
    Last edited by Elysia; 01-06-2008 at 09:36 AM.
    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.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Code:
        
    #include <cstdio>
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cctype>
    #include <vector>
    using namespace std;
    
    int main ()
    {
      ifstream file1;
      ifstream file2;
      //open file1
      vector<string> words1;
      file1.open ("1.txt");
      string str1;
      
      while( !file1.eof() )
      {
        getline (file1,str1);
        words1.push_back (str1);
      }
      
      //open file2
      vector<string> words2;
      file2.open ("2.txt");
      string str2;
      
      while( !file2.eof() )
      {
        getline (file2,str2);
        words2.push_back (str2);
      }
      int i=0 , j=0;
      while (i < ((int)words2.size()))
      {
         {
               if (words1[j] == words2[i])
               cout << words1[j] <<" is the same as " << words2[i]<< "\n";
         
               else
               cout << words1[j] <<" is not the same as " << words2[i]<< "\n";
         }
      i++;
      }
      i = 0; j++;
     
      while (i < ((int)words2.size()))
      {
         {
                if (words1[j] == words2[i])
                cout << words1[j] <<" is the same as " << words2[i]<< "\n";
         
                else
                cout << words1[j] <<" is not the same as " << words2[i]<< "\n"; 
         }
      i++;
      }  
      file1.close();
      file2.close();
      system("pause");
      return 0;
    }
    Thanks, i hope its better.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Sure, it's better. But not perfect. Not yet.
    First, make indentation consistent. Don't indent two spaces somewhere and 4 elsewhere. Choose one and stick with it. Alternatively, use a tab.
    Also keep blocks separate; indent them all - each block with an extra "indent":
    Code:
               if (words1[j] == words2[i])
               cout << words1[j] <<" is the same as " << words2[i]<< "\n";
    Should be:
    Code:
    		if (words1[j] == words2[i])
    			cout << words1[j] <<" is the same as " << words2[i]<< "\n";
    Code:
      while (i < ((int)words2.size()))
      {
      // ...
      i++;
      }
    This is also wrong. It's important that i++ is one more indent more than the while loop itself because it belongs TO the while loop and is not outside.
    So:
    Code:
      while (i < ((int)words2.size()))
    {
    // ...
    	i++;
    }
    And the while loops can be for loops:
    Code:
    for (i = 0; i < (int)words2.size(); i++)
    {
    	if (words1[j] == words2[i])
    		cout << words1[j] <<" is the same as " << words2[i]<< "\n";
    	else
    		cout << words1[j] <<" is not the same as " << words2[i]<< "\n"; 
    }
    And while we're at it, you could use arrays for this.

    Code:
    vector<ifstream> vFiles;
    vector< vector<string> > vWords;
    for (int i = 0; i < 2; i++)
    {
    	vFiles[i].open(/* My filename here */);
    	vWords.push_back( vector<string>() );
    	for (int j = 0; j < (int)vWords[i][j].size(); j++)
    	{
    		// Compare strings
    	}
    }
    Just an example.
    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.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Wow, thanks for your precious time Elysia, thats really nice of you.
    Im gonna try to take good note of this in my indentation in the future.

    I could use arrays for this?
    Im confused, cause you said to me the last time to avoid using arrays , rather using strings instead, being more secure.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Ah, but that was referring to char arrays.
    char myaray[50];
    It's better to use std::string for that.
    But there's nothing wrong with
    int n[50];
    Though if you need a dynamic array, you can use std::vector.

    If you know you have only two files, you can write:
    Code:
    vector<ifstream> vFiles;
    vector<string> vWords[2];
    for (int i = 0; i < 2; i++)
    {
    	vFiles[i].open(/* My filename here */);
    	//vWords.push_back( vector<string>() );
    	for (int j = 0; j < (int)vWords[i][j].size(); j++)
    	{
    		// Compare strings
    	}
    }
    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.

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Cool, thank you!

  12. #12
    The larch
    Join Date
    May 2006
    Posts
    3,573
    vector<ifstream> vFiles;
    You can't actually make a vector of streams because they are non-copyable. What you could do, however, is make a function to read a file into a vector of strings:

    Code:
    void read_file(const char* name, vector<string>& words)
    {
        ifstream fin(name);
        string line;
        words.clear();
        while (getline(fin, line)) {
            words.push_back(line);
        }
    }
    
    int main ()
    {
    
      vector<string> words1;
      vector<string> words2;
      
      read_file("1.txt", words1);
      read_file("2.txt", words2);
      ...
    Note that file reads shouldn't be controlled with eof(). In fact your program will read the last line twice if you do.

    As to the comparison part, you should be able to reduce redundant code using nested loops. Also, if you want to look at each item in a container, it might be easier to use a for loop:
    Code:
    for (size_t i = 0; i != words2.size(); ++i) {
        //loop body here
    }
    Also worth noting: rather than casting the return type of size(), one might use the correct type to compare it against in the first place (size_t i in this snippet).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  13. #13
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Thank you Anon!

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    So if I'm reading this right, you want to find all the words that appear in both files?

    In that case I suggest a number of things:
    1. Don't output the words that are different from each other, there will be masses of these compared to those found that are the same, as every word is not the same as almost every other word.
    2. Don't use a naive O(n*n) algorithm to find these. It will take increasingly forever as the file sizes increase. A better way to do it is to read in all words from both files and then sort both word lists then iterate through both lists comparing items in much the same way as you'd perform a mergesort. There are std library algorithms to do all this for you though, that make it extremely easy....

    First, read all words from each file into a set<string>
    Then use std_intersection to get all words that are in both files. Use set_symmetric_difference to find all words in either file that are not in both files if you wish.
    Note that this also takes care of any words that appear multiple times in the same file.

    Not only is this incredibly easy, it's incredibly fast thanks to the efficient algorithms used by the SC++L!
    Last edited by iMalc; 01-06-2008 at 11:44 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  15. #15
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Thank you iMalc for your useful input. I didnt even know about those functions.

    Actually this program is just a step toward the final program which is supposed to compare
    each word of file1 with each word of file2, that would be a sort of a dictionary and take some
    action on words in file1 if they were found in file2.

    But i guess this set_intersection funtion was made just for that. Thats really awesome.
    Last edited by Ducky; 01-07-2008 at 02:23 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  4. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  5. Replies: 2
    Last Post: 05-10-2002, 04:16 PM