Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

C++ - two int variables can't be compared.


  • Please log in to reply

#1
IO-error

IO-error

    Member

  • Member
  • PipPipPip
  • 276 posts
Hi all.

/offtopic
I used to be VBS nerd and made a lot of vbs scripts that helped me in my daily life.
One example is a vb-script that saves a picture with one press of a (logitech G15 macro-)key.
But now with the coming of Windows 7 and the disappearance of VBS overall, I need to learn C++.
/offtopic

This brings me to a problem in the source code.
I could post all files you need to create my program, but that's a bit clumsy, so I'll just give the relevant pieces of code and the includes I used.
The errorneous line is marked with stars for your convenience.

#include <windows.h>
#include <stdio.h>
#include <cstring>

#include <iostream>
#include <fstream>
#include "InputBox.h" //you probably don't have this file.

string s = result2;

int countVowels(char result2[]);
{
	int count = 0, k = 0;
	while (result2[k] != '\0');
	{
		  if (result2[k] == '\\');
		  {
			  count++;
		  }
		  k++;
	}
	return count;
}
int i = 0;
(line 108)******** for (i < count);********
		 (
		  i++;
		  int position = s.find("\\");
		  s.replace(position, 1, "\\\\");
		  }

Error messages:
108 F:\%path%\CInputBoxTest.cpp invalid operands of types `int' and `<unknown type>' to binary `operator<'
108 F:\%path%\CInputBoxTest.cpp expected `;' before ')' token
110 F:\%path%\CInputBoxTest.cpp expected `)' before ';' token
111 F:\%path%\CInputBoxTest.cpp expected primary-expression before "int"
111 F:\%path%\CInputBoxTest.cpp expected `)' before "int"


I understand I can't use < or any other >=! for comparing the two integers, but what do I use?
If you need more code, just ask me, I'll then add the .h and full .cpp.

***UPDATE after writing***
Whilst writing this (or typing, whatever), I googled across GetNumbers and GetBig... I might get into that, but if somebody has a solution to my initial problem, I'll be glad to hear it.

TIA & greet - I/O-Error
  • 0

Advertisements


#2
mpascal

mpascal

    Math Nerd

  • Retired Staff
  • 3,644 posts
Hi IO-error,

You can use < and > for comparing two integers. From what I can see, the function you are creating doesn't recognise what the variable "count" is.
  • 0

#3
IO-error

IO-error

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 276 posts
When I use either < or >, I get the same errorcode.
Line 108 - invalid operands of types `int' and `<unknown type>' to binary `operator<'

The sad thing is, I can't figure out why it says "count" is unknown.
I'll clean out the code and post it here ASAP.

Edited by IO-error, 14 November 2009 - 08:38 PM.

  • 0

#4
mpascal

mpascal

    Math Nerd

  • Retired Staff
  • 3,644 posts
Hi,

Try posting the whole code so I can take a look, or at least all the code up until line 115 or so.
  • 0

#5
IO-error

IO-error

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 276 posts
Thank you for your time mpascal.

I managed to fix the code by looking on the internet for clues all day long and trying diverse things....
I'm glad I can finally go to bed now.


This is how I fixed the faulty part.
I hope this helps other people who had the same problems.

google-tags: string replace all backslash C++

using std::string;
	  stringstream ss;
	  string s;
	  ss << result2;
	  ss >> s;

   int position = s.find( "\\" ); // find first space

   while ( position != string::npos ) 
   {
	  s.replace( position, 1, "\\\\" );
	  position = s.find( "\\", position + 2 );
   }

Edited by IO-error, 15 November 2009 - 11:58 AM.

  • 0

#6
mpascal

mpascal

    Math Nerd

  • Retired Staff
  • 3,644 posts
Hi,

Glad you fixed it :). Just out of curiosity, may I ask what the problem with the code was?
  • 0

#7
IO-error

IO-error

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 276 posts
I am sure the problem in first post was that I didn't use three parameters in s.replace().
As for the problems I had afterwards, it was just logics errors, which I had to crush my mind for to understand it.

Example, I didn't let the count of position slide after the position it has already been.
So basically:
s = "C:\ut2004\system\ut2004.exe"
and s needs to get all his \ replaced by \\.
The int_position told find() where to replace it and how many times.
But as the counter for replacing it went on, the counter for position remained the same.
thus ending with this:
s = "C:\\\\\\\\\\ut2004\system\ut2004.exe"

So when I finally did crack it I was very happy.
The program is now completed and dubbed KeyAdder 0.5 Beta.
The final release is not yet scheduled, but if you (google-guests) can't wait, contact me.
  • 0

#8
mpascal

mpascal

    Math Nerd

  • Retired Staff
  • 3,644 posts
Very interesting, thank you for that :)
  • 0

#9
IO-error

IO-error

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 276 posts
Thank you for your interest too.
Your post made me see that the variable "count" was not recognized, so I thought of merging the "find("\")"-function and "replace(npos,"\","\\")"-function into one function. And that, including the correct counting of npos, helped me out.

Thnx again for the kickstart.
  • 0

#10
mpascal

mpascal

    Math Nerd

  • Retired Staff
  • 3,644 posts
Not a problem, glad I could help you out :)
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP