Solved

Counter CGI Questions

Posted on 1998-08-04
30
475 Views
Last Modified: 2013-12-25
Well, I have this CGI script I made for a counter in C++.  It adds up all the digits, and then generates an HTML CODE STRING depeding on the results.  The problem is, that the main function, addone(), only runs ONCE in the program, and then the program terminates, but the variables are not kept, and therefore the counter will reset itself each time the program is run.  This is a problem, seeing as the counter will never reach beyond 1.  Could somone please explain to me how I could get it so the variables aren't reset every time the program is run?  Here is the code for the program (It is long and repetitive, I know.  :>  )

#include <iostream.h>
#include <string.h>

// Define main function

int addone(int one, int ten, int hun, int thou, int totl)
 {
  one++;
  if (one == 10 && ten == 99)
   {
    one = 0;
    ten = 1;
   }
  else if (one == 10 && ten != 99)
   {
    one = 0;
    ten++;

    if (ten == 10 && hun == 99)
     {
      ten = 0;
      hun = 1;
     }
    else if (ten == 109 && hun != 99)
     {
      ten = 0;
      hun++;

      if (hun == 10 && thou == 99)
       {
        hun = 0;
        thou = 1;
       }
      else if (hun == 10 && thou != 99)
       {
        hun = 0;
        thou++;

        if (thou == 10)
         {
          thou = 99;
          hun = 99 ;
          ten = 99 ;
          one = 1 ;
         }
       }
     }
   };
   return one, ten, hun, thou, totl;
 };

   main()
    {

    //  Create variables

     int total = 0;     // Total of counter
     int thousands = 99;  // Thousands Digit
     int hundreds = 99;   // Hundreds Digit
     int tens = 99;      // Tens digit
     int ones = 0;     // Ones digit
     char onecod[50];      // Ones code string
     char tencod[50];      // Tens code string
     char huncod[50];      // Hundreds code string
     char thocod[50];      // Thousands code string

     addone(ones, tens, hundreds, thousands, total);

     if (ones == 1)
      {
       onecod[50] = "<IMG SRC='one.gif'>";
      }
     else if (ones == 2)
      {
       onecod[50] = "<IMG SRC='two.gif'>";
      }
     else if (ones == 3)
      {
       onecod[50] = "<IMG SRC='three.gif'>";
      }
     else if (ones == 4)
      {
       onecod[50] = "<IMG SRC='four.gif'>";
      }
     else if (ones == 5)
      {
       onecod[50] = "<IMG SRC='five.gif'>";
      }
     else if (ones == 6)
      {
       onecod[50] = "<IMG SRC='six.gif'>";
      }
     else if (ones == 7)
      {
       onecod[50] = "<IMG SRC='seven.gif'>";
      }
     else if (ones == 8)
      {
       onecod[50] = "<IMG SRC='eight.gif'>";
      }
     else if (ones == 9)
      {
       onecod[50] = "<IMG SRC='nine.gif'>";
      }
     else
      {
       onecod[50] = "<IMG SRC='zero.gif'>";
      }
    if (tens == 1)
      {
       tencod[50] = "<IMG SRC='one.gif'>";
      }
     else if (tens == 2)
      {
       tencod[50] = "<IMG SRC='two.gif'>";
      }
     else if (tens == 3)
      {
       tencod[50] = "<IMG SRC='three.gif'>";
      }
     else if (tens == 4)
      {
       tencod[50] = "<IMG SRC='four.gif'>";
      }
     else if (tens == 5)
      {
       tencod[50] = "<IMG SRC='five.gif'>";
      }
     else if (tens == 6)
      {
       tencod[50] = "<IMG SRC='six.gif'>";
      }
     else if (tens == 7)
      {
       tencod[50] = "<IMG SRC='seven.gif'>";
      }
     else if (tens == 8)
      {
       tencod[50] = "<IMG SRC='eight.gif'>";
      }
     else if (tens == 9)
      {
       tencod[50] = "<IMG SRC='nine.gif'>";
      }
     else
     {
      tencod[50] = "";
     }
   if (hundreds == 1)
      {
       huncod[50] = "<IMG SRC='one.gif'>";
      }
     else if (hundreds == 2)
      {
       huncod[50] = "<IMG SRC='two.gif'>";
      }
     else if (hundreds == 3)
      {
       huncod[50] = "<IMG SRC='three.gif'>";
      }
     else if (hundreds == 4)
      {
       huncod[50] = "<IMG SRC='four.gif'>";
      }
     else if (hundreds == 5)
      {
       huncod[50] = "<IMG SRC='five.gif'>";
      }
     else if (hundreds == 6)
      {
       huncod[50] = "<IMG SRC='six.gif'>";
      }
     else if (hundreds == 7)
      {
       huncod[50] = "<IMG SRC='seven.gif'>";
      }
     else if (hundreds == 8)
      {
       huncod[50] = "<IMG SRC='eight.gif'>";
      }
     else if (hundreds == 9)
      {
       huncod[50] = "<IMG SRC='nine.gif'>";
      }
     else
     {
      huncod[50] = "";
     }
   if (thousands == 1)
      {
       thocod[50] = "<IMG SRC='one.gif'>";
      }
     else if (thousands == 2)
      {
       thocod[50] = "<IMG SRC='two.gif'>";
      }
     else if (thousands == 3)
      {
       thocod[50] = "<IMG SRC='three.gif'>";
      }
     else if (thousands == 4)
      {
       thocod[50] = "<IMG SRC='four.gif'>";
      }
     else if (thousands == 5)
      {
       thocod[50] = "<IMG SRC='five.gif'>";
      }
     else if (thousands == 6)
      {
       thocod[50] = "<IMG SRC='six.gif'>";
      }
     else if (thousands == 7)
      {
       thocod[50] = "<IMG SRC='seven.gif'>";
      }
     else if (thousands == 8)
      {
       thocod[50] = "<IMG SRC='eight.gif'>";
      }
     else if (thousands == 9)
      {
       thocod[50] = "<IMG SRC='nine.gif'>";
      }
     else
     {
      thocod[50] = "";
     }
   char codestring[900];
   strcpy (codestring, onecod);
   strcpy (codestring, tencod);
   strcpy (codestring, huncod);
   strcpy (codestring, thocod);

 return codestring;
}



Anyone out there able to help?



~Topace~
0
Comment
Question by:Topace
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 14
  • 9
  • 5
  • +1
30 Comments
 
LVL 10

Expert Comment

by:rbr
ID: 1831349
I think you should store the actual counter on a file and read it in at every call of your program.
0
 
LVL 1

Author Comment

by:Topace
ID: 1831350
Do you mean use fwrite() and fgts()?

~Topace~
0
 

Expert Comment

by:tangkh
ID: 1831351
You should read the current page value and increment it. Store it in a file perhaps is a good method, but certainly is not a common practice.
0
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
LVL 1

Author Comment

by:Topace
ID: 1831352
How would I go about reading the current page value?  I have to seperate it into ones, tens, hundreds, and thousands so that my program will generate the code.

Also --

When it generates the code, normal html code is like this: <IMG SRC="graphic.gif">

But, because the " character will stop the quotes in C++, I have to use '.  Will this still work?  If not, how can I use "  ?
If I were going to do an HTML tag like this:  OnClick="location='wherever'"
I would need BOTH the " and '.  How could I do this?


~Topace~
0
 
LVL 10

Expert Comment

by:rbr
ID: 1831353
To print " use \"
e.g.:
thocod[50] = "<IMG SRC=\"nine.gif\">";
0
 
LVL 1

Author Comment

by:Topace
ID: 1831354
Well, thanks for that information, I really did need it.  However, it does not supply the answer to my original question.  How would I be able to read the current total off of the web page?

~Topace~
0
 
LVL 1

Author Comment

by:Topace
ID: 1831355
How could I have the information read off of the webpage.  It would be easier, as I intend to use it for more than one webpage.  Is there a way I can get the C++ to read into Javascript on the webpage, so it will store the variables?

~Topace~
0
 
LVL 1

Author Comment

by:Topace
ID: 1831356
I need to know how to get my counter to work, not an answer about how to print.  Sorry.  :-(

~t~
0
 

Expert Comment

by:tangkh
ID: 1831357
I think rbr has supplied enough information to get the counter to work.
0
 
LVL 1

Author Comment

by:Topace
ID: 1831358
Not really, I still have no idea how to get the counter to read the current value off of the webpage.  Without it, it will start a 1 everytime the page is accessed.

~t~
0
 

Expert Comment

by:tangkh
ID: 1831359
Then use the suggested simplest method, create a text/binary file to store the value, and keep this file in your CGI directory. Everytime your counter programme is accessed, open the file and read the latest value and update it.
0
 
LVL 1

Accepted Solution

by:
jdyer earned 150 total points
ID: 1831360
You need to create a file and store the number. It's simple

in the main function open the file (eg "counter.txt" ) and fscanf a long value. Then take that long value and break it up like you are doing. This is the ONLY practical way to ensure persistence of your data!. Also, to print quotes use \" For example

printf( "\"Hello George,\" said the monkey." ); will print

"Hello George," said the monkey.

So there you go! use a file and read in the current value, and in your add one fopen it with the "w" flag and write a long into it!

main
{

long counter;

fh = fopen( "counter.txt", "r" );
fscanf( "%ld", &counter );
fclose( fh );

}

That's how is HAS to happen.

Hope this helps,
Regards,
  jdyer

PS rbr should get some points for this also!

addone( counter );

fh = fopen( "counter.txt", "w" );
fprintf( "%ld", counter + 1 );
fclose( fh );
0
 
LVL 1

Author Comment

by:Topace
ID: 1831361
Here is why this doesn't work.  I am using this as a free internet counter, available to my friends and associates.  They will be able to create their own graphics to use with it, but I am only going to use one cgi script because their server doesn't support CGI, and a copy of the script wouldn't run on their page.  So, if they ran the CGI script from the same directory, it will all be mushed into one, and the values will be off.  This is why I want to read it from the webpage.  Is there a way that depending on what page it comes from, it will use certain files to read from?


~t~
0
 
LVL 1

Expert Comment

by:jdyer
ID: 1831362
Sure, it will still work. You can create one file "counter.txt" for example and have the cgi request tag on an identifier. For example...

www.yourpage.com/counter.cgi?name=Bob

Then in counter.txt have the values for each person registered. For example.

Bob=1023
Chris=223
James=36785
David=7876
ETC...

Then just parse the file for the name supplied in the CGI request. That oughta be about the most elegant and appropriate way I can think of. Any other ideas from the rest of you out there?

For graphics, you could add a parameter on the CGI request specifying the graphic format they want to use. For example:

www.yourname.com/counter.cgi?name=Bob&style=BobNumbers

then you would use BobNumbers1.gif, BobNumbers2.gif, etc...

That should work beautifully!

Regards,
  jdyer
0
 
LVL 1

Expert Comment

by:jdyer
ID: 1831363
Re: your question about specifying the page it comes from, read my response above.
Their will be an environment variable QUERY_STRING that you will have to parse to get the parameters supplied. For example:

www.yourname.com/counter.cgi?name=Bob&style=BobNumbers

QUERY_STRING would equal: name=Bob&style=BobNumbers

for more info on how to parse this environment variable properly, do a search on Yahoo! for

CGI Programming Made Really Easy

That'll supply a lot more specific info. Hope this helps

Regards,
  jdyer
0
 
LVL 1

Author Comment

by:Topace
ID: 1831364
There are some functions I don't understand.  Like what does scanf() and fflush() do?
And what is the %ld?

~t~
0
 
LVL 1

Expert Comment

by:jdyer
ID: 1831365
Wow! You must be relatively new to C.

Okay scanf scans for a certain variable of the type you supply. For example:

char string[50];
scanf( "%s", string );

scans in a string from standard input. Other symbols are,

%d  // integer
%ld  // long
%f  // float
%lf  // double (long-float)
%s  // string

and some other less-common types. Read an ANSI Runtime Library Reference.

fflush() flushes the buffer of a specified stream.

fflush( stdout ) flushes everything in the buffer for the stdout stream to the screen. I think if you implement what I've suggested and read the webpage I directed you to that you will be fine. Good luck!

Regards,
  jdyer
0
 
LVL 1

Author Comment

by:Topace
ID: 1831366
There is a reason I don't understand.  I was taught C++ from a book ( anumber of them, actually) using iostream.h  I have never before used stdout, stdin, fflush(), printf(), etc.  How would UI use these functions using cout and cin?

~t~
0
 
LVL 1

Author Comment

by:Topace
ID: 1831367
Okay, I admit it, I am new to C++ (Kind of) I was self-taught, and so I was very limited to what I learned.  Is there a place I can go that will help me with this?

~t~
0
 
LVL 1

Expert Comment

by:jdyer
ID: 1831368
cout.flush() should work for flush.

open an input stream and do something like this:

istream is;
long number;

is << number;

would read in a long from the input stream. The input stream should be a file stream with the filename "counter.txt"

cin is equivalent to stdin
cout = stdout
cerr = stderr

Hope this helps.

Regards,
  jdyer
0
 
LVL 1

Author Comment

by:Topace
ID: 1831369
Holy smoly I am just realizing how much of this I didn't learn from the book.  First of all, how do I open an input stream?  I have been used to just using cin >> whatever when I want the user to input something.  Second, I have no idea what cerr is.  And last, I just want to be sure we are both talking about C++, because I don't know C, and the book told be it would be worse if I learned it because then I will learn all of the mistakes in C, too.      

I think I am going to need to find a website or something that will help me with this.  If you have a link, I would appreciate it.

~t~
0
 

Expert Comment

by:tangkh
ID: 1831370
In fact C or C++ doesnt make any difference. Most of the C commands are also applicable in C++.

Anyway, the where you structure your programme is a C method, i.e. a procedural method. The whole concept of C++ is to promote object-oriented modelling technique.
0
 
LVL 1

Expert Comment

by:jdyer
ID: 1831371
actually, I should have put

is >> number

Sorry, typing mistake. If you go to Yahoo and select through the categories in this order, you will find tons of links

Computers->Programming Languages->C and C++

Some are tutorials, check them out.

To open a file, and read in a number, here is a short example

long webCount;
ifstream ifs( "counter.txt" );     // input-file stream that opens counter.txt

if( ifs == 0 )     // file opened correctly
{
  ifs >> webCount;
}

Oh yeah, don't forget to #include <fstream.h>
Well, that oughta get you going.

Regards,

  jdyer

0
 
LVL 1

Expert Comment

by:jdyer
ID: 1831372
err, damn. Use if (!ifs) instead, I think that is standard. My compiler will handle

if( ifs == 0 )

But I'm not sure if that is standard or not
0
 

Expert Comment

by:tangkh
ID: 1831373
sure both if (ifs==0) and if(!ifs) are standard.
0
 
LVL 1

Author Comment

by:Topace
ID: 1831374
Okay, so, if I have:

istream is;

It opens a input stream called is, right?  (I have not gotten to input streams or anything in the second book I am reading on c++.  Is this part of polymorphism, because I haven't gotten to that yet.)
Then, if I have this:

long number;
istream is;

is >> number;

Would put whatever is in the instream into number?

~t~
0
 
LVL 1

Expert Comment

by:jdyer
ID: 1831375
yes that is correct... istream would read a long integer from the input stream and place it in "number"
0
 
LVL 1

Author Comment

by:Topace
ID: 1831376
Okay, got it.  Now, can you explain to me what the input stream is?  I mean, is it what the user has entered in?

~t~
0
 
LVL 1

Expert Comment

by:jdyer
ID: 1831377
The input stream would be a file input stream.

so you would open a file (for example, "counter.txt") with an input stream:

long number
ifstream ifcounter( "counter.txt" );

ifcounter >> number;

would read in a number from the file "counter.txt"

Regards,
  jdyer
0
 
LVL 1

Author Comment

by:Topace
ID: 1831378
Okay, I am beginning to understand.  Thanks for having patience.  So, the ">>" is the operator to read to something?  Kinda like assigning it to a variable?  

Anyway, ifcounter >> number will read the first line from counter.txt?  Or will it read the variable number from counter.txt?

~t~
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question