Debug assertion failed!

because of argv[1] i have this problem because when i replace it by any file name this code prints my correct answer

this is my code

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
#include "stack.h"
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <conio.h>
using namespace std;
enum precedence{plus,minus,times,divide,mod,eos,operand};
char expr[100];
precedence get_token(char *symbol,int *n)
{
	*symbol = expr[(*n)++];
	switch (*symbol)
	{
	case '+':
		return plus;
	case '-':
		return minus;
	case '/':
		return divide;
	case '*':
		return times;
	case '%':
		return mod;
	case '\0':
		return eos;
	default:
		return operand;
	}
}
int main(int argc,char* argv[])
{
	stack a;
	int m;
	precedence token;
	char symbol;
	ifstream myfile;
	myfile.open(argv[1]);
	if(myfile.is_open())
	{
		myfile>>m;
		for(int i=0;i<m;i++)
		{
			int op1=0;
			int op2=0;
			int count=0;
			int n=0;
			myfile>>expr;
			token=get_token(&symbol,&n);
			while(token!=eos)
			{
				if(token==operand)
				{ int r= atoi(&symbol);
					a.push(r);
					count++;
				}
				else
				{
					op2=a.pop();
					op1=a.pop();
					switch(token)
					{
					case plus:
						a.push(op1+op2);
						count--;
						break;
					case minus:
						a.push(op1-op2);
						count--;
						break;
					case times:
						a.push(op1*op2);
						count--;
						break;
					case divide:
						a.push(op1/op2);
						count--;
						break;
					case mod:
						a.push(op1%op2);
						count--;
						break;
					
					}
				}
				token=get_token(&symbol,&n);
			}
			if(count==1)
			{
			int y=a.pop();
			cout<<y<<endl;
			}
			else
				cout<<"Incorrect expression"<<endl;

		}
		
	}
	else
		cout<<"can not open file"<<endl;
	return 0;



}



stack is aclass i implemented it before and include it to this program
I do just the same and it works fine. If I had to guess, I'd guess you're going wrong with entering the name of the file to open on the command line.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>

int main (int argc, char *argv[])
{
  std::cout << "Opening file named " << argv[1] << std::endl;
  
  std::ifstream in(argv[1]);
  
  std::string word;
  
  in >> word;
  
  std::cout << " First word is " << word << std::endl;
  
  
  std::cout << "This program was called with " << argv[0];

return 0;

}
thx methodos &moschops for ur replies
but my problem isn't how to execute this program from the command window (tried to do it with another program and executed successfully)
this code when i try to debug it by visual studio 2008 it displays an error message says
Debug assertion failed
program:(the path of my execution file)
file:f:\dd\vctools\crt_bld\self_x86\crt\src\mbstowcs.c
line 69
expression s !=NULL

when trying to debug it step by step this message appears at the line of argv[1] but when i replace it by the name of my text file the code debug successful !!!

i thought that it may be from my stack but it work with the text file name normally !!!

Last edited on
Your program cannot be debugged. Period.
The reason is that the debugger needs a second parameter, argv[1], to be supplied to the program.
With your code, after the program is built, the debugger starts the program as "yourprogram.exe".
For your program to work, however, it needs two parameters. The first is argv[0] and that is "yourprogram.exe". The second parameter is argv[1], which is the file that needs to be opened "FileToBeOpened.extension".
Then,
1. make sure that both these files are in the same directory
2. open command promt
3. and type "yourprogram.exe FileToBeOpened.extension" in the right directory

Alternatively, replace argv[1] by the filename. The debugger will not need a second parameter. That's why it debugs fine as you already noted.
Have fun.
Last edited on
finally i knew my fault...... ooooooooooh it is a very stupid error
my execution file name have a space which the command window can't recognize the file name from the execution file name a very stupid error

sorry for disturbance and thnx alot :)
Topic archived. No new replies allowed.