control structure outputting wrong iterations via ints.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Thread: control structure outputting wrong iterations via ints.

  1. #1
    Join Date
    Mar 2013
    Posts
    25

    control structure outputting wrong iterations via ints.

    all,

    I have this:

    Code:
    			for (chr_min = 0; chr_min <= 24; chr_min += 3)
    			{
    				strOut = "";
    				for (ctr_row = 1; ctr_row <=3; ctr_row++)
    				{
    					switch (ctr_row)
    					{
    					case 1:
    						strOut = strOut + Line1.substr(chr_min, 3);
    						cout << ctr_row << endl;
    					case 2:
    						strOut = strOut + Line2.substr(chr_min, 3);
    						cout << ctr_row << endl;
    					case 3:
    						strOut = strOut + Line3.substr(chr_min, 3);
    						cout << ctr_row << endl;
    					}
    				}
    					cout << strOut << endl;
    			}
    it is outputting to the shell window, this:

    Code:
    1
    1
    1
    2
    2
    3
    and that pattern apparently repeats for every iteration that's specified in the outer loop. am I missing something here?

    I sure hope this is not because my CPP understanding is not up to par! thanks. any help surely appreciated.
    Last edited by ajetrumpet; March 18th, 2013 at 03:32 PM.

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,196

    Re: control structure outputting wrong iterations via ints.

    What's it supposed to do? What are Line1, 2 and 3? Have you run the program in the debugger?

  3. #3
    Join Date
    Apr 1999
    Posts
    27,444

    Re: control structure outputting wrong iterations via ints.

    Quote Originally Posted by ajetrumpet View Post
    am I missing something here?
    Where are your break statements in the switch-case?

    Regards,

    Paul McKenzie

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,517

    Re: control structure outputting wrong iterations via ints.

    Quote Originally Posted by Paul McKenzie View Post
    Where are your break statements in the switch-case?
    Good catch!
    Victor Nijegorodov

  5. #5
    Join Date
    Mar 2013
    Posts
    25

    Re: control structure outputting wrong iterations via ints.

    Quote Originally Posted by Paul McKenzie View Post
    Where are your break statements in the switch-case?

    Regards,

    Paul McKenzie
    I need them? wow, did not know that. Ya see? that's why I need *good documentation*!! I can't believe it. The redundancies in documentation is so convoluted...that's why I don't really like writing code. It's so easy yet people like scholars have made it sound like rocket science.

    thanks guys. yes, it's fixed.

    by the way, have any of you seen my thread on why the C++ documentation website is junk? maybe I shouldn't say so much...?

  6. #6
    Join Date
    Apr 1999
    Posts
    27,444

    Re: control structure outputting wrong iterations via ints.

    Quote Originally Posted by ajetrumpet View Post
    I need them? wow, did not know that. Ya see? that's why I need *good documentation*!!
    What book doesn't explain what break does? It is central in the way a switch/case works in C++.

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Mar 2013
    Posts
    25

    Re: control structure outputting wrong iterations via ints.

    well Paul,

    Unfortunately I'm a rare case of the individual that understands where all of this came from. So someone like me isn't really interested in reading books. There's no language out there that isn't just a massive set of rules. C++ is no different than any other language. There is nothing to learn about programming...all languages are the same. The only things that change are the leveraging features any given language has. Then you have the integration portion of it, and how it applies to all the other platforms/etc, etc... that are out there.

    That is why someone like me doesn't like reading the documentation because they have it in "teaching" format rather than in the format of raw data only. Raw data would be incredibly useful, but the average Joe can't learn that way. Understand?

    on a side note, if you're not already frustrated with my words, can you tell me all of the methods in CPP that can convert a string literal to an int?? Apparently there are many methods but this one I've gotten to work in a small 10 liner prog:

    *stoi(string); using <sstream> class.

    However, this code yields nothing in a bigger prog:

    Code:
    					for (ctr_array = 8; ctr_array = 0; ctr_array--)
    					{
    						num = stoi(strOut_number.substr(ctr_array, 1));
    						cout << num << endl;
    
    					}
    "num" is an int and "strOut_number" is a string. I have <sstream> referenced. any help here Paul?

    and please tell me I haven't screwed up the looping code! I'm googling it as I type this!
    Last edited by ajetrumpet; March 18th, 2013 at 09:19 PM.

  8. #8
    Join Date
    Mar 2013
    Posts
    25

    Re: control structure outputting wrong iterations via ints.

    if anyone is still listening, my "stoi" function is throwing an "abort called" error. I'm not really sure why but I'm still checking on it. I've narrowed it down to that.

  9. #9
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,591

    Re: control structure outputting wrong iterations via ints.

    Actually, you did screw up your loop code in the snippet from post #7: Your termination check expression is an assignment instead of a comparison, so it not only unintentionally modifies your loop control variable, it also unconditionally evaluates to false. So I strongly doubt the stoi() call inside that loop would ever throw any sort of exception or other runtime error, since it never gets executed.

    If you don't like books with a scholar attitude, and you're not going to learn C++ (that apparently you do not know perfectly by now) from the original ANSI/ISO standard document, which I wouldn' recommend to anyone at all for its high tideosity and boredom factor, I'd recommend to you Stroustrup's TC++PL. Due to its high level I do not recommend that book to beginners, but be warned that, yet, it's structured in chapters with increasing difficulty level that build upon each other, and it even does contain practice exercises.

    And one more recommended reading (sorry for this one ): http://www.cplusplus.com/doc/tutorial/control/

    I'd have to say quite some more, but typing is so tideous on the tablet...
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  10. #10
    Join Date
    Mar 2013
    Posts
    25

    Re: control structure outputting wrong iterations via ints.

    I know...it's tedious, and ubiquitous, and extrapiant, and incredulous....

    that's been fixed though. but back to my point, do you know where I can just get the relevant data for this language? again, RULES. is that even out there or do we actually have to deal with the accidental intellectualism as it stands now?

    that makes me sad if we have to. please tell me that is not the case!

  11. #11
    Join Date
    Mar 2013
    Posts
    25

    Re: control structure outputting wrong iterations via ints.

    one more piece of help to get me over the hump here?

    I have an array of strings like: array[10] = {"1","2","3","4"};

    I'm looking for them in a loop but I need to interpret them as integers. so if I see array[0] I need to capture that as 1, not "1".

    help again guys? I know this is pathetic...maybe I should consider changing careers...

  12. #12
    Join Date
    Apr 1999
    Posts
    27,444

    Re: control structure outputting wrong iterations via ints.

    Quote Originally Posted by ajetrumpet View Post
    well Paul,

    Unfortunately I'm a rare case of the individual that understands where all of this came from. So someone like me isn't really interested in reading books. There's no language out there that isn't just a massive set of rules. C++ is no different than any other language.
    We're talking about a switch/case. This is discussed in chapter 1 or 2 of any beginner C++ book.

    Missing these simple concepts is not trivial. It is very easy to create a C++ program that compiles with correct syntax, but when you run the program, totally unexpected things happen. You saw with your own example that you created a compilable and runnable program that did not execute what you expected.

    There is a big difference between C++ and other languages, and your example shows this difference. There are also cases where you create a program, and it runs differently (it may work on one machine, work differently on another machine, or maybe not at all) because of subtle errors. For example:
    Code:
    #include <iostream>
    int main()
    {
       int x;
       std::cout << x;
    }
    What gets printed? If you run this in "debug" mode in Visual C++, you get 0. When you run it as a release program, you may get 0, 2343, -9, etc. printed. Can you think of another language with this unpredictability, all because a variable is not initialized? Most other languages would either not compile at all, or at runtime, you get a stack trace, error box, whatever, telling you that the variable is uninitialized.

    Not so with C++. Instead, you get an angry customer calling you up saying that your program crashes, while when you run it in the shop, the program runs OK. This indicates that it is almost a given that C++ has to be learned in some sort of formal way (book, tutorial, classroom, etc.), and not in a haphazard type of fashion.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; March 18th, 2013 at 11:19 PM.

  13. #13
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,591

    Re: control structure outputting wrong iterations via ints.

    Well, if all you want actually is RULES, the standard document is for you. AFAIK the actual standard is only available for quite some bucks, but there may still be some of the drafts around for free. At any rate, I'd say, TC++PL is just about as authoritative as the standard, I'm not really sure, however, whether the C++11 version is out already. You'll still need to arrange with the structure of the book, against which you seem to have some sort of aversion I can't really understand, but at least the book has been written by the guy who created C++, so you'll hardly get anything better...
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  14. #14
    Join Date
    Apr 1999
    Posts
    27,444

    Re: control structure outputting wrong iterations via ints.

    Quote Originally Posted by ajetrumpet View Post
    one more piece of help to get me over the hump here?

    I have an array of strings like: array[10] = {"1","2","3","4"};

    I'm looking for them in a loop but I need to interpret them as integers. so if I see array[0] I need to capture that as 1, not "1".

    help again guys? I know this is pathetic...maybe I should consider changing careers...
    Look up atoi(), strtol(), strtoul(). For a "pure" C++ solution, you can use istringstream.

    Regards,

    Paul McKenzie

  15. #15
    Join Date
    Mar 2013
    Posts
    25

    Re: control structure outputting wrong iterations via ints.

    guys,

    I came from a VB legacy background, which I realize absolutely sucks because it basically takes the lower level "threaded concepts" (like differentiating between chars, strings, pointers, etc, etc...) about programming and basically dumbs it down 100% for beginners. VB doesn't bother to do things like that. Higher level languages do that I think and that is very unfortunate. let me just ask you guys this:

    if I have the following code in a VB environment, how do I convert this to Visual C++??

    Code:
    dim Line1 as string
    dim Line2 as string
    dim Line3 as string
    dim Line4 as string
    dim out_encrypt as string
    dim out_number as integer
    dim chr_min as integer
    dim out_encrypt as string
    dim ctr_row as integer
    dim ctr_array as integer
    dim checksum as integer
    dim ctr_checksum as integer
    
    dim acctSyms(9) as string 'base 0, 10 elements
    dim acctNums(9) as string 'base 0, 10 elements
    
    acctSyms(0)="|"
    acctSyms(1)="_"
    acctSyms(2)="||"
    acctSyms(3)="__"
    acctSyms(4)="|||"
    acctSyms(5)="___"
    acctSyms(6)="||||"
    acctSyms(7)="____"
    acctSyms(8)="|||||"
    acctSyms(9)="_____"
    
    acctNums(0)=0
    acctNums(1)=1
    acctNums(2)=2
    acctNums(3)=3
    acctNums(4)=4
    acctNums(5)=5
    acctNums(6)=6
    acctNums(7)=7
    acctNums(8)=8
    acctNums(9)=9
    
    'READ THE 4 LINES OF A TEXT FILE HERE...
    
    for chr_min = 0 to 24 step 3
    
    	out_encrypt = "";
    
    	for ctr_row = 1 to 3
    				
    		select case ctr_row
    					
    		case 1
    			out_encrypt = out_encrypt + mid(Line1, chr_min, 3)
    		case 2
    			out_encrypt = out_encrypt + mid(Line2, chr_min, 3)
    		case 3
    			out_encrypt = out_encrypt + mid(Line3, chr_min, 3)
    					
    		end select
    
    	next ctr_row
    
    		//find element that matches pipe symbol, concat acct number.
    		for ctr_array = 0 to 9
    					
    			if acctSyms(ctr_array) = out_encrypt then
    						
    				out_number = out_number + acctNums(ctr_array)
    				exit for
    						
    			end if
    
    		next ctr_array
    					
    		//calculate checksum, check for validity.
    		checksum = 0 //checksum formula driven by addition, thus baseline it with 0.
    		ctr_checksum = 1
    
    		for ctr_array = 8 to 0 step -1
    					
    			checksum = checksum + (mid(out_number, ctr_array + 1, 1) * ctr_checksum)
    			ctr_checksum = ctr_checksum + 1
    
    		next ctr_array
    					
    next chr_min
    the code is doing this:

    1) reading lines of symbols of a txt.
    2) translating those symbols into numbers.
    3) using a checksum formula to validate the string of actual numbers.

    I am going to try and download a converter online. anyone care to take a stab at this? the only thing I'm really missing is how to get the "outnumber" out of a string format and into an integer format so I can use the checksum formula on it.

    I'm sorry, I know this is kind of pathetic to be asking here. I've even been doing this for more than 5 years now!
    Last edited by ajetrumpet; March 19th, 2013 at 01:07 AM.

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 is a CodeGuru survey question.


Featured


HTML5 Development Center