[RESOLVED] code generation - Page 2
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 30 of 30

Thread: [RESOLVED] code generation

  1. #16
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,700

    Re: code generation

    what I mean is to call different 2d array with different size to the same function but not same time
    Yes. post #11

    Code:
    template<size_t N, size_t M>
    void random_letter(char (&array)[N][M])
    {
    	cout << N << endl << M << endl;
    }
    
    int main()
    {
    	char arr1[5][7];
    	random_letter(arr1);
    
            char arr2[8][9];
            random_letter(arr2);
    
           return 0;
    }
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.3.4)

  2. #17
    Join Date
    May 2017
    Posts
    173

    Re: code generation

    this code went in infinite loop on the value 5

    Code:
    template < size_t N , size_t M >
    
    void random_letter ( char ( &arrays ) [N][M] ) {
    
        srand ( time (0));
    	
    	char rnd = (rand() % 26) + 'A';
       
       cout << N << endl << M << endl;
    
    }

    and this is my case in main it should fill in with random numbers

    Code:
       case 1 : {
    	 
    	char arr1 [5][5];
    
    	random_letter ( arr1 );
    			
    
    }

  3. #18
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,700

    Re: code generation

    This works as expected and displays the size of the passed array.
    Code:
    #include <cstdlib>
    #include <ctime>
    
    template < size_t N, size_t M >
    void random_letter(char (&arrays)[N][M]) {
    
    	char rnd = (rand() % 26) + 'A';
    
    	cout << N << endl << M << endl;
    
    }
    
    int main()
    {
    	srand((unsigned int)time(0));
    
    	char arr1[5][5];
    
    	random_letter(arr1);
    }
    Note that random_letter() doesn't actually do anything - apart from displaying the array sizes. It doesn't change any of the values in the array arrays.

    If you have an infinite loop, what is happening in main() outside of the case? Have you debugged the program to find out the issue?

    Also note that srand() is only required to be called once at the start of he program.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.3.4)

  4. #19
    Join Date
    May 2017
    Posts
    173

    Re: code generation

    actually my program should send the array according to user choice
    let say I gave him 3 choices : 1- ....
    2-.....
    3-......

    and usr chose 1 for example which is my first array of size [5][5] I have to send it to function to fill random letter and display it to user you see ? each case array get another size but I have in case 1 I showed you an infinite loop on value 5

  5. #20
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,700

    Re: code generation

    Do you mean something like this?
    Code:
    #include <cstdlib>
    #include <ctime>
    #include <iostream>
    using namespace std;
    
    template < size_t N, size_t M >
    void random_letter(char (&arrays)[N][M]) {
    
    	for (size_t n = 0; n < N; ++n)
    		for (size_t m = 0; m < M; ++m)
    			arrays[n][m] = (rand() % 26) + 'A';
    
    }
    
    template < size_t N, size_t M >
    void display_array(char (&arrays)[N][M]) {
    
    	cout << N << " x " << M << endl;
    
    	for (size_t n = 0; n < N; ++n) {
    		for (size_t m = 0; m < M; ++m)
    			cout << arrays[n][m] << " ";
    
    		cout << endl;
    	}
    }
    
    int main()
    {
    	srand((unsigned int)time(0));
    	
    	int opt;
    
    	do {
    		cout << "Enter option (1, 2, 3 or 0 to quit): ";
    		cin >> opt;
    
    		switch (opt) {
    			case 1:
    				{
    					char arr[5][5];
    
    					random_letter(arr);
    					display_array(arr);
    				}
    				break;
    
    			case 2:
    				{
    					char arr[7][6];
    
    					random_letter(arr);
    					display_array(arr);
    				}
    				break;
    
    			case 3:
    				{
    					char arr[3][6];
    
    					random_letter(arr);
    					display_array(arr);
    				}
    				break;
    
    			case 0:
    				break;
    
    			default:
    				cout << "Invalid input" << endl;
    		}
    	} while (opt != 0);
    }
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.3.4)

  6. #21
    Join Date
    May 2017
    Posts
    173

    Re: code generation

    exactly men you got the point . thx for that

  7. #22
    Join Date
    May 2017
    Posts
    173

    Re: code generation

    I have a question plz what's the difference between putting the break in the case or outside of the case ?? cause on output I'm getting same result .Thank you

  8. #23
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,700

    Re: code generation

    Quote Originally Posted by david16 View Post
    I have a question plz what's the difference between putting the break in the case or outside of the case ?? cause on output I'm getting same result .Thank you
    break always goes with the case. You mean inside or outside of the {} ?
    In terms of the code in post #20, none! Personally, I put a break (or a return if applicable) at the very end of the case statements (unless I want a follow-on). In this case it could go at the end of the statements in the {}.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.3.4)

  9. #24
    Join Date
    May 2017
    Posts
    173

    Re: code generation

    I see . now if the user was stupid and pressed something else that number while choosing from the menu lets say a letter or pressed enter without writing anything how do I avoid infinite loop ?? I use cin.ignore and cin.fail ?? If yes , can you tell me where is the best place to add them . You see what I mean ?? Thx
    Last edited by david16; July 9th, 2017 at 08:37 AM.

  10. #25
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,700

    Re: code generation

    I use cin.ignore and cin.fail
    yes. But it's easier to use !cin than cin.fail.

    can you tell me where is the best place to add them
    yes. But try yourself first. That's how you learn! Hint. Consider another loop.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.3.4)

  11. #26
    Join Date
    May 2017
    Posts
    173

    Re: code generation

    ok I will try to do it my self then will come back

  12. #27
    Join Date
    May 2017
    Posts
    173

    Re: code generation

    Yes I got it . Since its a switch I can add it the default case like this

    Code:
    	default :
    
    		cout << "\nInvalid input!\n" << endl;
                  cin.clear();
                  cin.ignore(1000, '\n');
      }
    but still need one more thing and it will be complete . How to make it prompt invalid input or such message when user press enter without writing anything ?? I should use the !cin thing something like while ( !cin ) for example ?? thx
    Last edited by david16; July 9th, 2017 at 10:09 AM.

  13. #28
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,700

    Re: code generation

    How to make it prompt invalid input or such message when user press enter without writing anything
    You can't using stream extraction if you want this. That's the way it works. >> will wait until a non-white space char is entered as extraction ignores all white-space before a character.

    If you want to check for just <enter>, then you'll need something like using getline(), then check if the read string is empty or not. Then check if the entered value is valid or not.

    PS I'm not convinced that your solution to non-digit input is always correct. Have you tried it with different combinations of good and bad (invalid number and non-numeric) input?
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.3.4)

  14. #29
    Join Date
    May 2017
    Posts
    173

    Re: code generation

    yes it works
    Last edited by david16; July 9th, 2017 at 11:39 AM.

  15. #30
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,700

    Re: code generation

    [post moved to new thread http://forums.codeguru.com/showthrea...-c-dictionary] as relates to different topic]
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.3.4)

Page 2 of 2 FirstFirst 12

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)