In a question i am required to add every other number starting from the right most side and going left on every other number. I have a code that is almost right as i am led to beleive. the code is:

#include <iostream>
#include <cmath>


int main ()
{

  int i, cardNumber, checkSum = 0;

  // Compute checksum of every other digit starting from right-most digit
  for (i = cardNumber.Length - 1; i >= 0; i -= 2)
  {
    checkSum += (cardNumber[i] - 1);
	cout << checksum << endl;
  }

  // Now take digits not included in first checksum, multiple by two,
  // and compute checksum of resulting digits
  for (i = cardNumber.Length - 2; i >= 0; i -= 2)
  {
    int val = ((cardNumber[i] - 1) * 2);
    while (val > 0)
    {
      checkSum += (val % 10);
      val /= 10;
	  
    }
	cout << val << endl;
  }


  // Number is valid if sum of both checksums MOD 10 equals 0
 if ((checkSum % 10) == 0)
 {
	 cout << "Error not vailid";
 }


return 0;
}

the feed back that i recieve is:

1>------ Build started: Project: idontknow, Configuration: Debug Win32 ------
1>Compiling...

1>ddd.cpp

1>d:\my documents\visual studio 2008\projects\idontknow\idontknow\ddd.cpp(13) : error C2228: left of '.Length' must have class/struct/union

1> type is 'int'

1>d:\my documents\visual studio 2008\projects\idontknow\idontknow\ddd.cpp(15) : error C2109: subscript requires array or pointer type

1>d:\my documents\visual studio 2008

\projects\idontknow\idontknow\ddd.cpp(18) : error C2065: 'cout' : undeclared identifier

1>d:\my documents\visual studio 2008\projects\idontknow\idontknow\ddd.cpp(18) : error C2065: 'checksum' : undeclared identifier

1>d:\my documents\visual studio 2008

\projects\idontknow\idontknow\ddd.cpp(18) : error C2065: 'endl' : undeclared identifier

1>d:\my documents\visual studio 2008\projects\idontknow\idontknow\ddd.cpp(22) : error C2228: left of '.Length' must have class/struct/union

1> type is 'int'

1>d:\my documents\visual studio 2008\projects\idontknow\idontknow\ddd.cpp(24) : error C2109: subscript requires array or pointer type

1>d:\my documents\visual studio 2008\projects\idontknow\idontknow\ddd.cpp(31) : error C2065: 'cout' : undeclared identifier

1>d:\my documents\visual studio 2008\projects\idontknow\idontknow\ddd.cpp(31) : error C2065: 'endl' : undeclared identifier

1>d:\my documents\visual studio 2008\projects\idontknow\idontknow\ddd.cpp(38) : error C2065: 'cout' : undeclared identifier

1>Build log was saved at "file://d:\My Documents\Visual Studio 2008\Projects\idontknow\idontknow\Debug\BuildLog.htm"

1>idontknow - 10 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Recommended Answers

All 11 Replies

cardNumber.Length ? An integer doesn't have member elements.

1>d:\my documents\visual studio 2008\projects\idontknow\idontknow\ddd.cpp(13) : error C2228: left of '.Length' must have class/struct/union

1> type is 'int'

Since when does an int have a .Length member?

1>d:\my documents\visual studio 2008\projects\idontknow\idontknow\ddd.cpp(15) : error C2109: subscript requires array or pointer type

CardNumber is an int , not an array.

1>d:\my documents\visual studio 2008

\projects\idontknow\idontknow\ddd.cpp(18) : error C2065: 'cout' : undeclared identifier

Many of the rest are because you didn't specify the std namespace.

yeah i added in the namespace right after i posted it i forgot it but i did add it in after now we are only left with a few of them now trying to solve them out

uhmmm okay so update lol....

changed it to

#include <iostream>#include <cmath> using namespace std  int main (){   int i, cardNumber, checkSum = 0;   // Compute checksum of every other digit starting from right-most digit  for (i = cardNumber.Length - 1; i >= 0; i -= 2)  {    checkSum += (cardNumber[i] - 1);	cout << checksum << endl;  }   // Now take digits not included in first checksum, multiple by two,  // and compute checksum of resulting digits  for (i = cardNumber.Length - 2; i >= 0; i -= 2)  {    int val = ((cardNumber[i] - 1) * 2);    while (val > 0)    {      checkSum += (val % 10);      val /= 10;     }	cout << val << endl;  }    // Number is valid if sum of both checksums MOD 10 equals 0 if ((checkSum % 10) == 0) {	 cout << "Error not vailid"; }  return 0;}

and the feed back says:


1>d:\my documents\visual studio 2008\projects\idontknow\idontknow\ddd.cpp(18) : error C2228: left of '.Length' must have class/struct/union

1> type is 'int'

1>d:\my documents\visual studio 2008\projects\idontknow\idontknow\ddd.cpp(20) : error C2109: subscript requires array or pointer type

1>d:\my documents\visual studio 2008\projects\idontknow\idontknow\ddd.cpp(23) : error C2065: 'checksum' : undeclared identifier

1>d:\my documents\visual studio 2008\projects\idontknow\idontknow\ddd.cpp(27) : error C2228: left of '.Length' must have class/struct/union

1> type is 'int'

1>d:\my documents\visual studio 2008\projects\idontknow\idontknow\ddd.cpp(29) : error C2109: subscript requires

im not sure what it means when it says
left of '.Length' must have class/struct/union
subscript requires array or pointer type
'checksum' : undeclared identifier --> because i did identify it

Dave and I both answered that.

im not sure what it means when it says
left of '.Length' must have class/struct/union
subscript requires array or pointer type
'checksum' : undeclared identifier --> because i did identify it

It means "are you coming from a different language other than C or C++? Because your syntax ain't C or C++."

Is cardNumber supposed to be a structure? A string? What?

okay i fixed all of it except for one thing and i dont know what it means although i know you mentioned it....my code now is...

#include <iostream>
#include <cmath>  

using namespace std;

int main ()
{   
	int i; 
	int cardNumber;
	int checkSum=0;
	// Compute checksum of every other digit starting from right-most digit  
	for (i = cardNumber- 1; i >= 0; i -= 2)  
	{    
		checkSum += (cardNumber[i] - 1);	
		cout << checkSum << endl;  
	}   
	// Now take digits not included in first checksum, multiple by two,  
	// and compute checksum of resulting digits  
	for (i = cardNumber- 2; i >= 0; i -= 2)  
	{
		int val = ((cardNumber[i] - 1) * 2);
		while (val > 0)    
		{      
			checkSum += (val % 10);      
			val /= 10;     
		}	
		cout << val << endl;  
	}    
	// Number is valid if sum of both checksums MOD 10 equals 0 
	if ((checkSum % 10) == 0) 
	{	 
		cout << "Error not vailid"; 
	}  
	return 0;
}

and feed back says

error C2109: subscript requires array or pointer type

i have no idea what it means or how to go about solving it

and feed back says

error C2109: subscript requires array or pointer type

i have no idea what it means or how to go about solving it

It means that in order to have a subscript (i.e. ) you need to have an array, not a single integer

It looks like you could use some brushing up on datatypes and arrays.

What your first step should be is : convert an integer to a std::string (or char array) and go from there.

yeah i cant figure this out -_- lol

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.