Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 19-11-2009
Member
 
Join Date: Aug 2009
Posts: 40
Constant Variables in C++

In my code, when I replace the variable in the loop on line 11 by the content (T. Size () - S. Size ()), it no longer works. Yet T. Size () - S. Size () should always give the same thing, so why does not work without intermediate variable? How to change a constant variable in C++?
Code:
  # include <iostream>
 # include <stdio.h>
 # include <string>
 # include <vector>

 using namespace std;

 int count (const string & S, const String & T) (
     vector <int> Dp (S. size () + 1, 0); Dp [0] = 1;
 const int T = USELESS. size () - S. size ();
 for (int j = 0, j <= USELESS; j + +)
         for (int i = 0; i <S. size () + + i)
             if (S [i] == T [i + j]) (
                 Dp [i + 1] + = Dp [i];
                 Dp [i + 1]% = 10000;
             )
     return Dp [S. size ()];
 )

 int main () (
     int N; scanf ( "% d \ n", & N);
     for (int x = 1, X <= N, X + +) (
         string T; getline (cin, T);
         printf ( "Case #% d:% 04d \ n", X, count ( "welcome to code", T));
     )
 )
Reply With Quote
  #2  
Old 19-11-2009
Member
 
Join Date: May 2008
Posts: 2,293
Re: Constant Variables in C++

It is simple in concept, variables declared with ‘const’ added become constants and cannot be altered by the program, There are no 'Constant Variables' in C. A constant [expression] can't be a variable. Its a datum that is not an object. It has no address.
Reply With Quote
  #3  
Old 19-11-2009
Member
 
Join Date: Dec 2008
Posts: 177
Re: Constant Variables in C++

size_t == unsigned int (or unsigned integer), so the conversion problem can be avoided, an unsigned int is implicitly converted to int if necessary.The error is with your test case 3: T.size () - s.size () = unsigned (+ infinity ), so your program crashes. That's why it is necessary to convert unsigned to signed.
Reply With Quote
  #4  
Old 19-11-2009
Member
 
Join Date: Jan 2009
Posts: 199
Re: Constant Variables in C++

Static const variables cannot be changed. You would normally use them to store hard limits like the maximum length of a file name etc. Why would you want to change the value of a CONSTANT? when is supposed to be constant..... Just declare that variable as an integer or something different than a constant.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Constant Variables in C++"
Thread Thread Starter Forum Replies Last Post
What are the String Variables in PHP? N I C K Software Development 4 28-01-2010 08:28 PM
What are the PHP Variables? shivendra Software Development 5 28-01-2010 07:05 PM
Replace variables in php John Wilson Software Development 3 04-12-2009 02:17 PM
Variables in .bat files jackz Software Development 3 30-07-2009 10:33 PM
Variables to use in many forms! RupaliP Software Development 3 19-02-2009 11:41 PM


All times are GMT +5.5. The time now is 06:21 AM.