last string is always the answer

This is a discussion on last string is always the answer within the C++ Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <string> #include "simpio.h" #include "genlib.h" #include "strlib.h" int main() { string longestString, current; int work = ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    7

    last string is always the answer

    Code:
    #include <stdio.h>
    #include <string>
    #include "simpio.h"
    #include "genlib.h"
    #include "strlib.h"
    int main() {
    	string longestString, current;
    	int work = 0;
    	int possible;
    	int greatest;
    	printf("please enter a list of strings, end the list with the word 'end'");
    	printf("enter: ");
    	current = GetLine();
    	longestString = current;
    	greatest = StringLength(current);
    	while(work == 0) {
    	printf("enter: ");
    	current = GetLine();
    		if (StringEqual(current, "end"))
    		{ 
    			work++;
    		}
    		else if (StringLength(current) > greatest)
    		{
    			greatest = possible; 
    			longestString = current;
    		}
    		else {
    
    		}
    	}
    	printf("the longest string is: %s\n", current);
    
    
    }
    it should put out the longest string but it always says end is longest...wtf?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Because you are printing the current string? If you want to print the longest string, then you should print longestString instead.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,545
    There is something that is seriously wrong with that code...
    I fail to see "using namespace std;" anywhere, and I still see printf instead of std::cout. Plus I see StringEqual (probably uses strcmp or something) and StringLength (probably uses strlen or something).
    If you are going to use C++, then you should stick to it and not C.

    And lastly, try writing a flowchart or pseudo-code sometime to see what you are actually doing.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please check my C++
    By csonx_p in forum C++ Programming
    Replies: 263
    Last Post: 07-24-2008, 09:20 AM
  2. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  3. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  4. "Operator must be a member function..." (Error)
    By Magos in forum C++ Programming
    Replies: 16
    Last Post: 10-28-2002, 01:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21