acess violatione error

i keep getting an acess violation. it has to do with my parameters, or initialising of parametrers. can anyone get my code to work?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <iostream>
#include <sstream>
#include <stdio.h>
#include <cstdlib>
#include <string>
#include <string.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>

using namespace std;

stringstream stst;
string str ;
int cpp_do_ousb_command(string *str) //===========
{ FILE *fpipe ;
if ( !(fpipe = (FILE*)popen(str->c_str(),"r")))
{ cout << "pipe error" << endl ;
exit(0) ;
}

char line[100] ;
fgets( line, sizeof line, fpipe) ;
pclose(fpipe) ;
*str = line ; // caller can see whole ousb string in str.
return( atoi(line)) ; // return int version of string.
}


int main(int argc, char *argv[])
{
    
    const double MAXRANGE = 100; //maxrange
    const double MINRANGE = 0;//minrange

int pwn_freq;

    int pwm_duty;

    double param1=atoi(argv[1]);
    double param2=atoi(argv[2]);
    

  if (argc == 1)  // no parameters print this line.
    { cout << "3333435, s3333435@student.rmit.edu.au, Matthew Merigan"<< endl ;
      
      return(0) ;
    }
     
    if(argc>3)
    {
      cout<<"P"<<endl;
      return(0);
    }
     
    if (strcmp(argv[1],"P") && strcmp(argv[1],"M") && strcmp(argv[1],"L") && strcmp(argv[1],"A"))
    {
       cout<<"I"<<endl;
       return 0;
    }               

/*hey its james why don’t we just use 

default:
           cout<<"I";
in stead of the code i’ve highlighted above “default” make all the values not present in the cases go to default were were we would have cout<<"I"; 

agreed.*/

     switch (argv[1][0])
    {
       case 'P':
       {

if(param2<MINRANGE||param2>MAXRANGE)
{
cout<<"r"<<endl;
return(0);
}
pwn_freq = 46 ; 
pwm_duty = 46 ; //changed
stst.str("") ; // clear if used before.
stst << pwn_freq ;
// note -r below ensures reply is just an integer.
str = "ousb -r pwm-freq 1 " + stst.str() ;
cout << cpp_do_ousb_command(&str) << endl ;

stst << pwm_duty ;
str = "ousb -r pwm 1 " + stst.str() ;
cout << cpp_do_ousb_command(&str) << "p"<<endl ;
     
     break;
   }
   
   case 'L':
       {
     
     break;
   }
   
   case 'A':
       {
     
     break;
   }
   
   case 'M':
       {
     
     break;
   }
default:

{

cout<<    "I"<<endl;
break;
}
}
   
system ("pause");
return(0) ;
}
One could call this kind of post an access violation.

You need to be much more specific (what, where, when, error message...)
Well, the first access violation you might encounter is here:

1
2
    double param1=atoi(argv[1]);
    double param2=atoi(argv[2]);


Unless you always provide at least two command line parameters!

As it stand, running with less than two command line parameters will feel a null pointer to one or other of the atoi() calls.

If running without command line parameters, you need to add logic (using argc) to control your command line handling. I think you might need more logic further down, but I didn't look too closely.

Andy

P.S. "Real" C++ programmers prefer to pass by reference not pointer!
you are right it is the seting of the parameters was where i found the problem, but how do i set them properly to make them logical?

sorry again

MGM
Topic archived. No new replies allowed.