Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #58704 > unrolled thread

strange problem

Started bybilsch <king621@comcast.net>
First post2015-03-23 02:10 +0000
Last post2015-03-23 21:47 +0000
Articles 20 on this page of 34 — 12 participants

Back to article view | Back to comp.lang.c


Contents

  strange problem bilsch <king621@comcast.net> - 2015-03-23 02:10 +0000
    Re: strange problem Richard Heathfield <rjh@cpax.org.uk> - 2015-03-23 03:03 +0000
      Re: strange problem Keith Thompson <kst-u@mib.org> - 2015-03-22 22:09 -0700
        Re: strange problem Richard Heathfield <rjh@cpax.org.uk> - 2015-03-23 09:04 +0000
      Re: strange problem bilsch <king621@comcast.net> - 2015-03-23 08:30 +0000
        Re: strange problem Richard Heathfield <rjh@cpax.org.uk> - 2015-03-23 09:06 +0000
          Re: strange problem bilsch <king621@comcast.net> - 2015-03-23 21:21 +0000
            Re: strange problem Richard Heathfield <rjh@cpax.org.uk> - 2015-03-23 21:42 +0000
              Re: strange problem bilsch <king621@comcast.net> - 2015-03-23 22:35 +0000
              Re: strange problem Tim Rentsch <txr@alumni.caltech.edu> - 2015-03-24 12:20 -0700
            Re: strange problem Keith Thompson <kst-u@mib.org> - 2015-03-23 15:23 -0700
              Re: strange problem Richard Heathfield <rjh@cpax.org.uk> - 2015-03-23 23:14 +0000
                Re: strange problem Keith Thompson <kst-u@mib.org> - 2015-03-23 17:10 -0700
    Re: strange problem glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-03-23 03:03 +0000
      Re: strange problem bilsch <king621@comcast.net> - 2015-03-23 08:37 +0000
        Re: strange problem raltbos@xs4all.nl (Richard Bos) - 2015-03-23 09:37 +0000
          Re: strange problem bilsch <king621@comcast.net> - 2015-03-23 21:27 +0000
            Re: strange problem Richard Heathfield <rjh@cpax.org.uk> - 2015-03-23 21:43 +0000
              Re: strange problem bilsch <king621@comcast.net> - 2015-03-23 22:36 +0000
                Re: strange problem Keith Thompson <kst-u@mib.org> - 2015-03-23 17:09 -0700
        Re: strange problem JohnF <john@please.see.sig.for.email.com> - 2015-03-23 13:02 +0000
          Re: strange problem Richard Heathfield <rjh@cpax.org.uk> - 2015-03-23 13:10 +0000
            Re: strange problem Robert Wessel <robertwessel2@yahoo.com> - 2015-03-23 19:18 -0500
              Re: strange problem Richard Heathfield <rjh@cpax.org.uk> - 2015-03-24 00:28 +0000
                Re: strange problem Robert Wessel <robertwessel2@yahoo.com> - 2015-03-23 19:38 -0500
                Re: strange problem jt@toerring.de (Jens Thoms Toerring) - 2015-03-24 01:12 +0000
          Re: strange problem Keith Thompson <kst-u@mib.org> - 2015-03-23 08:10 -0700
          Re: strange problem Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-03-23 11:28 -0400
            Re: strange problem bilsch <king621@comcast.net> - 2015-03-23 21:47 +0000
              Re: strange problem Kaz Kylheku <kaz@kylheku.com> - 2015-03-23 21:53 +0000
              Re: strange problem Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-03-23 18:34 -0400
                Re: strange problem James Kuyper <jameskuyper@verizon.net> - 2015-03-23 18:58 -0400
      Re: strange problem raltbos@xs4all.nl (Richard Bos) - 2015-03-23 09:54 +0000
    Re: strange problem Kaz Kylheku <kaz@kylheku.com> - 2015-03-23 21:47 +0000

Page 1 of 2  [1] 2  Next page →


#58704 — strange problem

Frombilsch <king621@comcast.net>
Date2015-03-23 02:10 +0000
Subjectstrange problem
Message-ID<mensmn$rrs$1@dont-email.me>
Please see code below.  The intention is to execute the 'else' path if 
there are any command line arguments.  If no arguments then execute the 
'if' path (hard coded values).  Two weeks ago that is how it worked but 
now it takes the 'else' path even when no arguments.  As shown I've tried 
testing either argv[1] and argc but neither works.  Does anyone know what 
might be wrong?
TIA.    Bill S.


int main(int argc, char *argv[]){

      

//if(argv[1] == NULL)
if(argc < 1)

{ delay=1;

  glength = 666;

  mode = 2;
  k1 = 1.0;
}

else	

{

printf("delay between pixel plotting: ");

scanf("%d", &delay);

printf("graph length in pixels x 111: ");

scanf("%d", &glength);

glength = 111 * glength;

printf("enter 1 for window, enter 2 for full screen: ");

scanf("%d", &mode);   

if (mode < 1)

mode = 1;	//accepts any integer, converts to range

if (mode > 1)

mode = 2;

printf("scaling factor: ");

scanf("%f", &k1);   

}

[toc] | [next] | [standalone]


#58706

FromRichard Heathfield <rjh@cpax.org.uk>
Date2015-03-23 03:03 +0000
Message-ID<menvoi$t61$1@dont-email.me>
In reply to#58704
On 23/03/15 02:10, bilsch wrote:
> Please see code below.  The intention is to execute the 'else' path if
> there are any command line arguments.  If no arguments then execute the
> 'if' path (hard coded values).  Two weeks ago that is how it worked but
> now it takes the 'else' path even when no arguments.  As shown I've tried
> testing either argv[1] and argc but neither works.  Does anyone know what
> might be wrong?
> TIA.    Bill S.
>

Firstly, you're missing a prototype for printf. Add:

#include <stdio.h>

But that isn't the problem you're asking about.

>
> int main(int argc, char *argv[]){
>
> //if(argv[1] == NULL)
> if(argc < 1)

Although it is legal for argc to be 0, it's rare, and it's very unlikely 
that argc will be 0 on your system. If argc is >= 1, then argv[0] will 
contain (the address of the first character of) a string that somehow 
represents the program name, which you're going to get whether you 
supply command line arguments or not (assuming you're starting the 
program from the command line, not via some exotic system call from 
another program).

So you probably mean this line to be:

if(argc <= 1)

or

if(argc < 2)

> { delay=1;
>    glength = 666;
>    mode = 2;
>    k1 = 1.0;

You don't appear to have definitions for any of these objects.

> }
> else	
> {
> printf("delay between pixel plotting: ");

If you don't force the program to flush the output stream, either by 
appending a newline to your printf or explicitly calling fflush(stdout), 
you have no guarantee that the prompt will appear before the program 
blocks for input. I suggest that you change this to either:

printf("delay between pixel plotting: ");
fflush(stdout);

or

printf("delay between pixel plotting:\n");


> scanf("%d", &delay);

This function gives you very little in the way of detecting invalid 
inputs. Consider, for example, a user who types in "help" at this stage. 
How would you be able to take appropriate action? How could you even 
detect that the user hadn't entered a valid number?

Consider capturing *all* input in string form (hint: fgets) and then 
parsing it yourself using, for example, strtoul in this case.

Similar considerations apply to subsequent uses of printf and scanf in 
the remainder of your program.

> printf("graph length in pixels x 111: ");
> scanf("%d", &glength);
> glength = 111 * glength;

Assuming glength is an int, what happens if the user enters a value > 
INT_MAX / 111 ? (Answer: the behaviour of the program is undefined after 
the multiplication takes place.)

<snip>

-- 
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within

[toc] | [prev] | [next] | [standalone]


#58716

FromKeith Thompson <kst-u@mib.org>
Date2015-03-22 22:09 -0700
Message-ID<87vbhsti44.fsf@kst-u.example.com>
In reply to#58706
Richard Heathfield <rjh@cpax.org.uk> writes:
> On 23/03/15 02:10, bilsch wrote:
[...]
>> scanf("%d", &delay);
>
> This function gives you very little in the way of detecting invalid
> inputs. Consider, for example, a user who types in "help" at this
> stage. How would you be able to take appropriate action? How could you
> even detect that the user hadn't entered a valid number?

scanf returns the number of items it was able to read.  In this case, if
it returns 1 then a number will have ben stored in `delay`; otherwise
there was some kind of error (perhaps the user typing "help").

Which is not to say that scanf is a particularly good way to read
numeric input, but it's probably good enough for simple programs that
don't have to be robust in the presence of bad input.

[...]

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#58722

FromRichard Heathfield <rjh@cpax.org.uk>
Date2015-03-23 09:04 +0000
Message-ID<meokt7$r1n$1@dont-email.me>
In reply to#58716
On 23/03/15 05:09, Keith Thompson wrote:
> Richard Heathfield <rjh@cpax.org.uk> writes:
>> On 23/03/15 02:10, bilsch wrote:
> [...]
>>> scanf("%d", &delay);
>>
>> This function gives you very little in the way of detecting invalid
>> inputs. Consider, for example, a user who types in "help" at this
>> stage. How would you be able to take appropriate action? How could you
>> even detect that the user hadn't entered a valid number?
>
> scanf returns the number of items it was able to read.  In this case, if
> it returns 1 then a number will have ben stored in `delay`; otherwise
> there was some kind of error (perhaps the user typing "help").

Yes, this function gives you very little in the way of detecting invalid 
input, doesn't it? :-) But, since the OP doesn't catch the return value, 
he won't be able to detect that the user hasn't entered a valid number.

> Which is not to say that scanf is a particularly good way to read
> numeric input, but it's probably good enough for simple programs that
> don't have to be robust in the presence of bad input.

We can agree to disagree about that. I don't think it's good enough.

-- 
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within

[toc] | [prev] | [next] | [standalone]


#58717

Frombilsch <king621@comcast.net>
Date2015-03-23 08:30 +0000
Message-ID<meoivf$rrs$2@dont-email.me>
In reply to#58706

> if(argc < 2)
> 

> 
> printf("delay between pixel plotting:\n");
> 
>
I did these. It still doesn't work. I had originally: if(argv[1]==NULL) 
which, two weeks ago, correctly selected the path with the hard coded 
values when no argument included on the command line.  That doesnt work 
either now.  This is truely a strange problem. 

[toc] | [prev] | [next] | [standalone]


#58723

FromRichard Heathfield <rjh@cpax.org.uk>
Date2015-03-23 09:06 +0000
Message-ID<meol0s$r1n$2@dont-email.me>
In reply to#58717
On 23/03/15 08:30, bilsch wrote:
>
>
>> if(argc < 2)
>>
>
>>
>> printf("delay between pixel plotting:\n");
>>
>>
> I did these. It still doesn't work.

The code you posted last time wouldn't even compile, let alone work.

This is not a difficult problem. It really isn't. But you won't fix it 
by showing us a different problem, which is what you're doing at the moment.

> I had originally: if(argv[1]==NULL)
> which, two weeks ago, correctly selected the path with the hard coded
> values when no argument included on the command line.  That doesnt work
> either now.  This is truely a strange problem.

No, it isn't. The real problem is that you are not showing us your real 
code. That's not a strange problem. On the contrary, it's a very common 
problem.

-- 
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within

[toc] | [prev] | [next] | [standalone]


#58786

Frombilsch <king621@comcast.net>
Date2015-03-23 21:21 +0000
Message-ID<meq04j$be9$1@dont-email.me>
In reply to#58723
Here's the whole program.  It uses SDL2 library for graphics.

/*  program sinexx.cpp 3/22/15.  compile: g++ sinexx.cpp -o sinexx -std=c+
+11 -Wall -lSDL2 */



#include <SDL2/SDL.h>

#include <stdio.h>

#include <iostream>

#include <math.h>

#define PI 3.14159





static int display_in_use = 0;  // Only using first display 

SDL_DisplayMode vmode;		// display mode structure

 

int maxx=0, maxy=0, delay, lcount=1, restart=0, n1=0, n2=0, flag1=0, 
flag2=0;      

int glength, mode, x2, y2, c1=0, c2=0, c3=255;

float f1, f2, f3, y, a1=0.003, a2=4.1946, t, k1=1.0;



int rgb[12]={0,0,255,204,0,204,255,0,153,204,0,204};	//color rgb 
triplets





int main(int argc, char *argv[]){

      

	if(argv[1] == NULL)

	//if(argc < 2)

	{ delay=1;

	  glength = 666;

	  mode = 2;

	  k1 = 1.0; }

	else	

x200:;	{

	printf("delay between pixel plotting: ");

	scanf("%d", &delay);



	printf("graph length in pixels x 111: ");

	scanf("%d", &glength);

	glength = 111 * glength;



	printf("enter 1 for window, enter 2 for full screen: ");

	scanf("%d", &mode);   

   	if (mode < 1)

	mode = 1;	//accepts any integer, converts to range

	if (mode > 1)

	mode = 2;



	printf("scaling factor: ");

	scanf("%f", &k1);   

	}



SDL_Init(SDL_INIT_VIDEO);



//SDL_GetDisplayMode(display_in_use, 0, &vmode);

//vmode.w = maxx; //max x pixel

//vmode.h = maxy; //max y pixel



//if (mode == 1)

//SDL_Window *win1 = SDL_CreateWindow("", 0, 0, 640, 480, 
SDL_WINDOW_SHOWN);

//if (mode == 2)

//SDL_Window *win1 = SDL_CreateWindow("", 0, 0, maxx, maxy, 
SDL_WINDOW_SHOWN);





SDL_Window *win1 = NULL;

if (mode == 1)

win1 = SDL_CreateWindow("", 0, 0, 640, 480, SDL_WINDOW_SHOWN);

if (mode == 2)

win1 = SDL_CreateWindow("", 0, 0, 1366, 768, SDL_WINDOW_SHOWN);

// win1 = SDL_CreateWindow("", 0, 0, maxx, maxy, SDL_WINDOW_SHOWN);



SDL_Renderer* ren = NULL;

ren =  SDL_CreateRenderer( win1, 0, SDL_RENDERER_ACCELERATED);

SDL_SetRenderDrawColor( ren, 0, 0, 0, 255 );    //set render to black for 
background

SDL_RenderClear( ren );				// Clear window



SDL_Event event;       // The event structure



  	while(1)

	{

          if(SDL_PollEvent(&event) != 0 )  //SDL_PE = 1 if key hit

           {  const Uint8 *state = SDL_GetKeyboardState(NULL);

	      if (state[SDL_SCANCODE_RETURN]) 

	      goto x100;   //  quit if return key hit

	      if (state[SDL_SCANCODE_BACKSLASH])

	      { restart = 1;  //restart flag

		goto x100;

	      }

	   }

	       

	flag1 = 0;

	flag2 = 0;

	   while (flag1 == 0)      // flag1=1 black pxl plotted glength 
pxl behind color pxl

           {if (flag2 == 0)    // flag2=1 means color pxl plotted

                {t  = k1*(lcount+glength);

              //   c3 = 255;

	         flag2 = 1;

	        }

	     else

	        { t  = k1*lcount;

		  c1 = 0;

		  c2 = 0;

	          c3 = 0;

	          flag1 = 1;

	        }

		f1 = sin(a1*PI*t);

		f2 = sin(a2*a1*PI*t);

		f3 = f1 + f2;

		y =  exp(-f3);

		if(mode==1)

		{ x2 = (int)((200 * f1)+350);

		  y2 = (int)(60 * y);

		}

		else

		{	

		  x2 = (int)((500 * f1)+650);	//   scale up for full 
screen

		  y2 = (int)(80 * y)+80;

                }

SDL_SetRenderDrawColor(ren,c1,c2,c3,255);  //  plot pixels

SDL_RenderDrawPoint(ren,x2,y2);

SDL_RenderPresent(ren);



          }	// end of while(flag1) loop



SDL_Delay(delay);			//  delay between pixels



	lcount++;

	if (lcount == 10000)		

{lcount=1;

SDL_SetRenderDrawColor( ren, 0, 0, 0, 255 );	//  clears the screen 
after 10,000 pixels

SDL_RenderClear(ren);

}

   	c1=rgb[(3*n1)+0];			//  gets shades from array

	c2=rgb[(3*n1)+1];

	c3=rgb[(3*n1)+2];

	n2++;

	if(n2==15)			//  15 pixels per shade

	{n2=0;

	n1++;}

	if(n1==4)			//  4 shades in array  

	n1=0;      





}    //  end of while(1) loop



x100:;

SDL_DestroyRenderer(ren);	//  clean up

SDL_DestroyWindow(win1);

SDL_Quit();

if(restart==1)	//  restart=0  goes to end

  {restart=0;

   goto x200;

  }

}

[toc] | [prev] | [next] | [standalone]


#58789

FromRichard Heathfield <rjh@cpax.org.uk>
Date2015-03-23 21:42 +0000
Message-ID<meq1ah$ipi$1@dont-email.me>
In reply to#58786
On 23/03/15 21:21, bilsch wrote:
>
> Here's the whole program.  It uses SDL2 library for graphics.
>
<snip>

>
> int main(int argc, char *argv[]){
>
> 	if(argv[1] == NULL)
> 	//if(argc < 2)

Messing with these will make no difference whatsoever.

> 	{ delay=1;
> 	  glength = 666;
> 	  mode = 2;
> 	  k1 = 1.0; }
> 	else	
> x200:;

The else ends here, with the semicolon after your label.

The next brace:
	{

opens a new local scope which is called in all circumstances. That's 
your problem.

To fix this, remove the label and semicolon, or move them inside the 
local scope.

<snip>

-- 
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within

[toc] | [prev] | [next] | [standalone]


#58798

Frombilsch <king621@comcast.net>
Date2015-03-23 22:35 +0000
Message-ID<meq4f6$be9$4@dont-email.me>
In reply to#58789
On Mon, 23 Mar 2015 21:42:28 +0000, Richard Heathfield wrote:

> On 23/03/15 21:21, bilsch wrote:
>>
>> Here's the whole program.  It uses SDL2 library for graphics.
>>
> <snip>
> 
> 
>> int main(int argc, char *argv[]){
>>
>> 	if(argv[1] == NULL)
>> 	//if(argc < 2)
> 
> Messing with these will make no difference whatsoever.
> 
>> 	{ delay=1;
>> 	  glength = 666;
>> 	  mode = 2;
>> 	  k1 = 1.0; }
>> 	else
>> x200:;
> 
> The else ends here, with the semicolon after your label.
> 
> The next brace:
> 	{
> 
> opens a new local scope which is called in all circumstances. That's
> your problem.
> 
> To fix this, remove the label and semicolon, or move them inside the
> local scope.
> 
> <snip>
 Yes!!!  That solved the problem.  Thanks much.

[toc] | [prev] | [next] | [standalone]


#58866

FromTim Rentsch <txr@alumni.caltech.edu>
Date2015-03-24 12:20 -0700
Message-ID<kfnlhimfbie.fsf@x-alumni2.alumni.caltech.edu>
In reply to#58789
Richard Heathfield <rjh@cpax.org.uk> writes:

> On 23/03/15 21:21, bilsch wrote:
>
>> Here's the whole program.  It uses SDL2 library for graphics.
>
> <snip>
>
>> int main(int argc, char *argv[]){
>>
>> 	if(argv[1] == NULL)
>> 	//if(argc < 2)
>
> Messing with these will make no difference whatsoever.
>
>> 	{ delay=1;
>> 	  glength = 666;
>> 	  mode = 2;
>> 	  k1 = 1.0; }
>> 	else	
>> x200:;
>
> The else ends here, with the semicolon after your label.
>
> The next brace:
>     {
>
> opens a new local scope which is called in all circumstances.
> That's your problem.  [snip]

A nice illustration of one of the weaknesses of inferior
bracing styles.

[toc] | [prev] | [next] | [standalone]


#58795

FromKeith Thompson <kst-u@mib.org>
Date2015-03-23 15:23 -0700
Message-ID<87vbhrs68q.fsf@kst-u.example.com>
In reply to#58786
bilsch <king621@comcast.net> writes:
> Here's the whole program.  It uses SDL2 library for graphics.

Is this the program that works, or the program that doesn't work?

After deleting the "#include <iostream>" and re-joining some comment
lines that were wrapped by somebody's news software, I was able to
compile it.

Incidentally, if you were able to compile that program with the
"#include <iostream>" line, then you were probably using a C++ compiler.
I suggest using a C compiler to compile C code.  You might be able to
persuade your compiler to act as a C compiler with the right setting.

Removing the code that's not relevant to your question leaves us with:

#include <stdio.h>
int main(int argc, char *argv[]) {
    if (argv[1] == NULL) {
        puts("argv[1] == NULL, use default values");
    }
    else {
        puts("argv[1] != NULL, get values from argv");
    }
}

This works correctly for me: when I run it with no command line
arguments, it prints the first message, and when I run it with one or
more arguments it prints the second.

This version also works correctly (and I personally find it clearer):

#include <stdio.h>
int main(int argc, char *argv[]) {
    if (argc < 2) {
        puts("argc < 2, use default values");
    }
    else {
        puts("argc >= 2, get values from argv");
    }
}

Remember that argc is the number of valid elements in the array that
argv points to (more precisely, argv points to its first element).
argv[0] is the command name, so if there are no arguments you'll
normally have argc==1, argv[0] pointing to a string representing the
command name, and argv[1]==NULL.  It is admittedly a little confusing
that the value of argc is one greater than the number of command-line
arguments; just remember that the count includes the program name.

Now what exactly is the problem you're having with the code you've
posted?  How are you invoking your program, and how does it behave?
(You might also try one of the smaller programs I've posted here.)

If you're on Windows, spaces either in the name of the executable, or in
the name of a directory in the path you're using, or in the arguments
you're passing to it might be an issue.  Adding double quotes on the
command line might work around that.  (If that's the issue, a
Windows-specific forum might be more helpful.)

Recommended reading: http://sscce.org/

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#58803

FromRichard Heathfield <rjh@cpax.org.uk>
Date2015-03-23 23:14 +0000
Message-ID<meq6mh$c43$1@dont-email.me>
In reply to#58795
On 23/03/15 22:23, Keith Thompson wrote:

<snip>

> Removing the code that's not relevant to your question leaves us with:
>
> #include <stdio.h>
> int main(int argc, char *argv[]) {
>      if (argv[1] == NULL) {
>          puts("argv[1] == NULL, use default values");
>      }
>      else {
>          puts("argv[1] != NULL, get values from argv");
>      }
> }

Unfortunately, you made the same mistake that blisch originally made. In 
removing the code that wasn't relevant to his problem, you managed to 
remove some code that was highly relevant to his problem. :-)

<snip>

-- 
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within

[toc] | [prev] | [next] | [standalone]


#58807

FromKeith Thompson <kst-u@mib.org>
Date2015-03-23 17:10 -0700
Message-ID<87mw33s1af.fsf@kst-u.example.com>
In reply to#58803
Richard Heathfield <rjh@cpax.org.uk> writes:
> On 23/03/15 22:23, Keith Thompson wrote:
> <snip>
>
>> Removing the code that's not relevant to your question leaves us with:
>>
>> #include <stdio.h>
>> int main(int argc, char *argv[]) {
>>      if (argv[1] == NULL) {
>>          puts("argv[1] == NULL, use default values");
>>      }
>>      else {
>>          puts("argv[1] != NULL, get values from argv");
>>      }
>> }
>
> Unfortunately, you made the same mistake that blisch originally
> made. In removing the code that wasn't relevant to his problem, you
> managed to remove some code that was highly relevant to his
> problem. :-)

Indeed I did.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#58707

Fromglen herrmannsfeldt <gah@ugcs.caltech.edu>
Date2015-03-23 03:03 +0000
Message-ID<menvqm$sts$1@speranza.aioe.org>
In reply to#58704
bilsch <king621@comcast.net> wrote:
> Please see code below.  The intention is to execute the 'else' path if 
> there are any command line arguments.  If no arguments then execute the 
> 'if' path (hard coded values).  Two weeks ago that is how it worked but 
> now it takes the 'else' path even when no arguments.  As shown I've tried 
> testing either argv[1] and argc but neither works.  Does anyone know what 
> might be wrong?

http://crasseux.com/books/ctutorial/argc-and-argv.html
 
> int main(int argc, char *argv[]){
>      
> //if(argv[1] == NULL)
> if(argc < 1)
> { delay=1;
>  glength = 666;
>  mode = 2;
>  k1 = 1.0;
> }
> else    

You could make it argc<=1, for example.

-- glen

[toc] | [prev] | [next] | [standalone]


#58718

Frombilsch <king621@comcast.net>
Date2015-03-23 08:37 +0000
Message-ID<meojc0$rrs$3@dont-email.me>
In reply to#58707
> 
> You could make it argc<=1, for example.

I tried  that just now.  It doesnt fix the problem.  This is truely a 
strange problem.

[toc] | [prev] | [next] | [standalone]


#58727

Fromraltbos@xs4all.nl (Richard Bos)
Date2015-03-23 09:37 +0000
Message-ID<550fdeaa.863531@news.xs4all.nl>
In reply to#58718
bilsch <king621@comcast.net> wrote:

> I tried  that just now.  It doesnt fix the problem.  This is truely a 
> strange problem.

Yes, it is; it's very strange how beginners insist on posting
incompilable code and then claim that "it worked before".

_Post your real code_.

Richard

[toc] | [prev] | [next] | [standalone]


#58787

Frombilsch <king621@comcast.net>
Date2015-03-23 21:27 +0000
Message-ID<meq0f5$be9$2@dont-email.me>
In reply to#58727
Here's the whole program.  It uses SDL2 library for graphics.

/*  program sinexx.cpp 3/22/15.  compile: g++ sinexx.cpp -o sinexx -std=c+
+11 -Wall -lSDL2 */



#include <SDL2/SDL.h>

#include <stdio.h>

#include <iostream>

#include <math.h>

#define PI 3.14159





static int display_in_use = 0;  // Only using first display 

SDL_DisplayMode vmode;		// display mode structure

 

int maxx=0, maxy=0, delay, lcount=1, restart=0, n1=0, n2=0, flag1=0, 
flag2=0;      

int glength, mode, x2, y2, c1=0, c2=0, c3=255;

float f1, f2, f3, y, a1=0.003, a2=4.1946, t, k1=1.0;



int rgb[12]={0,0,255,204,0,204,255,0,153,204,0,204};	//color rgb 
triplets





int main(int argc, char *argv[]){

      

	if(argv[1] == NULL)

	//if(argc < 2)

	{ delay=1;

	  glength = 666;

	  mode = 2;

	  k1 = 1.0; }

	else	

x200:;	{

	printf("delay between pixel plotting: ");

	scanf("%d", &delay);



	printf("graph length in pixels x 111: ");

	scanf("%d", &glength);

	glength = 111 * glength;



	printf("enter 1 for window, enter 2 for full screen: ");

	scanf("%d", &mode);   

   	if (mode < 1)

	mode = 1;	//accepts any integer, converts to range

	if (mode > 1)

	mode = 2;



	printf("scaling factor: ");

	scanf("%f", &k1);   

	}



SDL_Init(SDL_INIT_VIDEO);



//SDL_GetDisplayMode(display_in_use, 0, &vmode);

//vmode.w = maxx; //max x pixel

//vmode.h = maxy; //max y pixel



//if (mode == 1)

//SDL_Window *win1 = SDL_CreateWindow("", 0, 0, 640, 480, 
SDL_WINDOW_SHOWN);

//if (mode == 2)

//SDL_Window *win1 = SDL_CreateWindow("", 0, 0, maxx, maxy, 
SDL_WINDOW_SHOWN);





SDL_Window *win1 = NULL;

if (mode == 1)

win1 = SDL_CreateWindow("", 0, 0, 640, 480, SDL_WINDOW_SHOWN);

if (mode == 2)

win1 = SDL_CreateWindow("", 0, 0, 1366, 768, SDL_WINDOW_SHOWN);

// win1 = SDL_CreateWindow("", 0, 0, maxx, maxy, SDL_WINDOW_SHOWN);



SDL_Renderer* ren = NULL;

ren =  SDL_CreateRenderer( win1, 0, SDL_RENDERER_ACCELERATED);

SDL_SetRenderDrawColor( ren, 0, 0, 0, 255 );    //set render to black for 
background

SDL_RenderClear( ren );				// Clear window



SDL_Event event;       // The event structure



  	while(1)

	{

          if(SDL_PollEvent(&event) != 0 )  //SDL_PE = 1 if key hit

           {  const Uint8 *state = SDL_GetKeyboardState(NULL);

	      if (state[SDL_SCANCODE_RETURN]) 

	      goto x100;   //  quit if return key hit

	      if (state[SDL_SCANCODE_BACKSLASH])

	      { restart = 1;  //restart flag

		goto x100;

	      }

	   }

	       

	flag1 = 0;

	flag2 = 0;

	   while (flag1 == 0)      // flag1=1 black pxl plotted glength 
pxl behind color pxl

           {if (flag2 == 0)    // flag2=1 means color pxl plotted

                {t  = k1*(lcount+glength);

              //   c3 = 255;

	         flag2 = 1;

	        }

	     else

	        { t  = k1*lcount;

		  c1 = 0;

		  c2 = 0;

	          c3 = 0;

	          flag1 = 1;

	        }

		f1 = sin(a1*PI*t);

		f2 = sin(a2*a1*PI*t);

		f3 = f1 + f2;

		y =  exp(-f3);

		if(mode==1)

		{ x2 = (int)((200 * f1)+350);

		  y2 = (int)(60 * y);

		}

		else

		{	

		  x2 = (int)((500 * f1)+650);	//   scale up for full 
screen

		  y2 = (int)(80 * y)+80;

                }

SDL_SetRenderDrawColor(ren,c1,c2,c3,255);  //  plot pixels

SDL_RenderDrawPoint(ren,x2,y2);

SDL_RenderPresent(ren);



          }	// end of while(flag1) loop



SDL_Delay(delay);			//  delay between pixels



	lcount++;

	if (lcount == 10000)		

{lcount=1;

SDL_SetRenderDrawColor( ren, 0, 0, 0, 255 );	//  clears the screen 
after 10,000 pixels

SDL_RenderClear(ren);

}

   	c1=rgb[(3*n1)+0];			//  gets shades from array

	c2=rgb[(3*n1)+1];

	c3=rgb[(3*n1)+2];

	n2++;

	if(n2==15)			//  15 pixels per shade

	{n2=0;

	n1++;}

	if(n1==4)			//  4 shades in array  

	n1=0;      





}    //  end of while(1) loop



x100:;

SDL_DestroyRenderer(ren);	//  clean up

SDL_DestroyWindow(win1);

SDL_Quit();

if(restart==1)	//  restart=0  goes to end

  {restart=0;

   goto x200;

  }

}

[toc] | [prev] | [next] | [standalone]


#58790

FromRichard Heathfield <rjh@cpax.org.uk>
Date2015-03-23 21:43 +0000
Message-ID<meq1br$ipi$2@dont-email.me>
In reply to#58787
On 23/03/15 21:27, bilsch wrote:
> Here's the whole program.  It uses SDL2 library for graphics.

<snip>

> x200:;

This is still your problem.

<snip>

-- 
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within

[toc] | [prev] | [next] | [standalone]


#58799

Frombilsch <king621@comcast.net>
Date2015-03-23 22:36 +0000
Message-ID<meq4i4$be9$5@dont-email.me>
In reply to#58790
On Mon, 23 Mar 2015 21:43:09 +0000, Richard Heathfield wrote:

> On 23/03/15 21:27, bilsch wrote:
>> Here's the whole program.  It uses SDL2 library for graphics.
> 
> <snip>
> 
>> x200:;
> 
> This is still your problem.
> 
> <snip>

Thanks.  I responded to your first post with a thank you.

[toc] | [prev] | [next] | [standalone]


#58806

FromKeith Thompson <kst-u@mib.org>
Date2015-03-23 17:09 -0700
Message-ID<87r3sfs1cv.fsf@kst-u.example.com>
In reply to#58799
bilsch <king621@comcast.net> writes:
> On Mon, 23 Mar 2015 21:43:09 +0000, Richard Heathfield wrote:
>> On 23/03/15 21:27, bilsch wrote:
>>> Here's the whole program.  It uses SDL2 library for graphics.
>> 
>> <snip>
>> 
>>> x200:;
>> 
>> This is still your problem.
>> 
>> <snip>
>
> Thanks.  I responded to your first post with a thank you.

I think his point is that you posted the entire program twice.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.c


csiph-web