GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 15-Jun-2012, 12:14
Rsk08 Rsk08 is offline
New Member
 
Join Date: Jun 2012
Posts: 6
Rsk08 is on a distinguished road

Assertion expression error (str != NULL)


Hi, im doing a program that converts from a roman number to a numerical number and im trying to write on the screen and also on a file that i created, the program shows no erros opens and when i enter the the numbers to convert it shows me the assertion error i think its related to the fprintf but im not sure hope someone could help im a begginer at this, heres the code:

CPP / C++ / C Code:
void main(){
	 FILE *fp;
	 int k, *Arabe, ne;
	 char **NRomano;
	
         system("cls");		
	 fp = fopen("C:\\romano.txt","w");
			
	 printf("Quantos numeros romanos quer converter?: ");
	 scanf("%d", &ne);
						
	 NRomano = (char**) malloc (ne * sizeof(char*));
	 Arabe = (int*) malloc (ne * sizeof(int));

	 printf("\n\n");

	 for (k=0; k<ne; k++) 
	 NRomano[k] = (char*) malloc(ne * sizeof(char));

	 for (k=0; k<ne; k++) {
		printf("Digite o numero romano: "); 
		fflush(stdin);
		gets(NRomano[k]);	
		Arabe[k] = teclado(NRomano[k]);
							
	}
	printf("\n\n");
	for (k=0; k<ne; k++){
	        printf("O numero romano %s = %d em arabe\n", NRomano[k],   Arabe[k]);		
		fprintf(fp,"O numero romano %s = %d em arabe\n", NRomano[k], Arabe[k]);
	}
}
Last edited by admin : 16-Jun-2012 at 13:53. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #2  
Old 15-Jun-2012, 17:11
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,147
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Assertion expression error (str != NULL)


Quote:
Originally Posted by Rsk08
... begginer at this...

OK, here's the deal:

For you to have a reasonable expectation of getting meaningful help in learning how to debug programs like this, I recommend the following:
  1. Post a complete program that we can try to compile and execute. Show what headers you use. Show all functions that are in your program. That's what I mean by "complete."
    The most important function may be the translation function, which you omitted from your post. If you don't want to show the world what you did inside teclado(), I can understand that. Just put in a "stub" (a fake function that returns a value of 1 or some such thing), so that we (and you) can see whether other parts of the program can run OK. Maybe doing this can even help you get to the bottom of things yourself.

  2. If you suspect that fprintf() is causing problems, just comment out anything that deals with the output file. If the problem persists, you have completed an important step in debugging: eliminating certain parts of the program from consideration. If the trouble goes away when you omit the fprintf() stuff and you don't understand what is wrong, then, maybe we can help you "drill down" into your code and help you discover what is causing the problem.

  3. Tell us exactly what inputs you gave the program. If possible, paste your session into your post.

  4. Show us exactly what happened. Did it get some output and then crash? (Was output that it did give correct?) Did it just crash out of the chute with no indication at all? (Did the output file get created?) Paste program output, including run-time error messages into your post.

  5. Tell us what version compiler and what version operating system you are using. Sometimes it makes a difference to people who would like to help. If you got any messages (warnings or whatever) from the compiler, paste them into your post.



Regards,

Dave
__________________
Sometimes I just can't help myself...
  #3  
Old 15-Jun-2012, 17:35
Rsk08 Rsk08 is offline
New Member
 
Join Date: Jun 2012
Posts: 6
Rsk08 is on a distinguished road

Re: Assertion expression error (str != NULL)


ok, i´ll try to explain it better, so here's all my code:

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
	

	
int teclado(char NRomano[]){
	
		int tam = strlen(NRomano);
		int	arabe = 0;
		for (int i=0; i<tam; i++) {
			switch (NRomano[i]) {
				case 'V': arabe = arabe + 5;
					break;
				case 'L': arabe = arabe + 50;
					break;
				case 'D': arabe = arabe + 500;
					break;
				case 'M': arabe = arabe + 1000;
					break;
				case 'I': 
					if (i == tam-1) 
						arabe = arabe + 1;
					else
						if (NRomano[i+1] != 'I') 
							arabe = arabe - 1;
						else 	
							arabe = arabe + 1;
					break;
				case 'X':
					if (i == tam-1 ) 
						arabe = arabe + 10;
						else 
							if (( NRomano[i+1] != 'I') && (NRomano[i+1] != 'V') && (NRomano[i+1] != 'X')) 
								arabe = arabe - 10;
							else 
								arabe = arabe + 10;
					break;
				case 'C': 
					if (i == tam-1) 
						arabe = arabe + 100;
						else 
							if (( NRomano[i+1] != 'M') && ( NRomano[i+1] != 'D')) 
							arabe = arabe + 100;
						else 
							arabe = arabe - 100;
					break;
			}

		}
		
	return arabe;
}

void main() {

	char escolha, op;

	do{
		system("cls");
		printf("Conversao de numeros romanos para arabe !!\t");
		printf("\n\n");
		printf("Como quer fornecer os dados?\n\n");
		printf("---------------------------------------\n");
		printf("|                                                       |\n");
		printf("|    Por Ficheiro                   [1] |\n");
		printf("|    Por Teclado                  [2] |\n");
		printf("|    Deseja Sair do Programa ?    [3] |\n");
		printf("|                                     |\n");
		printf("---------------------------------------\n\n");
		printf("Qual e a sua escolha?: ");
		fflush(stdin);
		scanf("%c", &escolha);

		switch(escolha){
			/*case '1': 
				FILE *f;
				int  ne, Arabe, i;
				char **NRomano;
				char drive[10], ficheiro[20];
				
					system("cls");
					printf("Qual a drive?\n");
					fflush(stdin);
					gets(drive);
					strcat(drive,":\\");
					printf("Qual o nome do ficheiro?\n");
					fflush(stdin);
					gets(ficheiro);
					strcat(drive, ficheiro);
					f= fopen(drive, "wt");
					
					if (f == NULL)
						puts("Impossivel abrir ficheiro");
						exit(1);
					else 
						printf("Quantos numeros romanos quer converter?: ");
						scanf(" %d", &ne);

						Arabe = (int*) malloc (ne * sizeof(int));
						NRomano = (char**) malloc (ne * sizeof(char*));

						printf("\n\n");
						for (i=0; i<ne; i++) 
							NRomano[i] = (char*) malloc(ne * sizeof(char));
							
						
						for (i=0; i<ne; i++) {
							printf("Digite o numero romano: "); 
							fflush(stdin);
							gets(NRomano[i]);
							Arabe[i] = teclado(NRomano[i]);
						}
						printf("\n\n");
						for (i=0; i<ne; i++){
							fprintf(
					
						
						
		
				break;*/
			case '2':

					FILE *fp;
					int k, *Arabe, ne;
					char **NRomano;
	

						system("cls");		
						fp = fopen("C:\\romano.txt","w");
			
						printf("Quantos numeros romanos quer converter?: ");
						scanf("%d", &ne);
						
						NRomano = (char**) malloc (ne * sizeof(char*));
						Arabe = (int*) malloc (ne * sizeof(int));
						printf("\n\n");

						for (k=0; k<ne; k++) 
							NRomano[k] = (char*) malloc(ne * sizeof(char));

						for (k=0; k<ne; k++) {
							printf("Digite o numero romano: "); 
							fflush(stdin);
							gets(NRomano[k]);	
							Arabe[k] = teclado(NRomano[k]);
							
						}
						printf("\n\n");
						for (k=0; k<ne; k++){
							printf("O numero romano %s = %d em arabe\n", NRomano[k], Arabe[k]);		
							fprintf(fp,"O numero romano %s = %d em arabe\n", NRomano[k], Arabe[k]);
						}
				
				break;
			case '3': exit(1);
				break;
		}
		printf("\n");
		printf("Deseja Sair?\n");
		fflush(stdin);
		scanf("%c", &op);
	}while( op == 'n');
}
Last edited by admin : 16-Jun-2012 at 13:54. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #4  
Old 15-Jun-2012, 17:54
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,147
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Assertion expression error (str != NULL)


Quote:
Originally Posted by Rsk08
ok, i´ll try to explain it better, so here's all my code:...

Hmmm. I see the code. but it can't be compiled with my compiler:
Compiler messages (GNU gcc version 4.1.2 on my Centos 5.8 Linux workstation) are
Code:
gcc -Wall -W -pedantic --std=c99 zz.c -o zz zz.c:55: warning: return type of ‘main’ is not ‘int’ zz.c: In function ‘main’: zz.c:124: error: expected expression before ‘FILE’ zz.c:130: error: ‘fp’ undeclared (first use in this function) zz.c:130: error: (Each undeclared identifier is reported only once zz.c:130: error: for each function it appears in.)

I hate to repeat myself, but suggestion number 1 was to submit a program that we can compile and execute. I mean, you previously had a program that you could compile and execute, right?


I don't see any explanation or responses to my other suggestions.

Were any parts of my previous suggestions unclear?


Would you like to try one more time?


Regards,

Dave
__________________
Sometimes I just can't help myself...
  #5  
Old 15-Jun-2012, 17:59
Rsk08 Rsk08 is offline
New Member
 
Join Date: Jun 2012
Posts: 6
Rsk08 is on a distinguished road

Re: Assertion expression error (str != NULL)


ok, i´ll try to explain it better, so here's all my code:

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
	

	
int teclado(char NRomano[]){
	
		int tam = strlen(NRomano);
		int	arabe = 0;
		for (int i=0; i<tam; i++) {
			switch (NRomano[i]) {
				case 'V': arabe = arabe + 5;
					break;
				case 'L': arabe = arabe + 50;
					break;
				case 'D': arabe = arabe + 500;
					break;
				case 'M': arabe = arabe + 1000;
					break;
				case 'I': 
					if (i == tam-1) 
						arabe = arabe + 1;
					else
						if (NRomano[i+1] != 'I') 
							arabe = arabe - 1;
						else 	
							arabe = arabe + 1;
					break;
				case 'X':
					if (i == tam-1 ) 
						arabe = arabe + 10;
						else 
							if (( NRomano[i+1] != 'I') && (NRomano[i+1] != 'V') && (NRomano[i+1] != 'X')) 
								arabe = arabe - 10;
							else 
								arabe = arabe + 10;
					break;
				case 'C': 
					if (i == tam-1) 
						arabe = arabe + 100;
						else 
							if (( NRomano[i+1] != 'M') && ( NRomano[i+1] != 'D')) 
							arabe = arabe + 100;
						else 
							arabe = arabe - 100;
					break;
			}

		}
		
	return arabe;
}

void main() {

	char escolha, op;

	do{
		system("cls");
		printf("Conversao de numeros romanos para arabe !!\t");
		printf("\n\n");
		printf("Como quer fornecer os dados?\n\n");
		printf("---------------------------------------\n");
		printf("|                                                    |\n");
		printf("|    Por Ficheiro                           [1] |\n");
		printf("|    Por Teclado                           [2] |\n");
		printf("|    Deseja Sair do Programa ?        [3] |\n");
		printf("|                                                    |\n");
		printf("---------------------------------------\n\n");
		printf("Qual e a sua escolha?: ");
		fflush(stdin);
		scanf("%c", &escolha);

		switch(escolha){
			/*case '1': 
				FILE *f;
				int  ne, Arabe, i;
				char **NRomano;
				char drive[10], ficheiro[20];
				
					system("cls");
					printf("Qual a drive?\n");
					fflush(stdin);
					gets(drive);
					strcat(drive,":\\");
					printf("Qual o nome do ficheiro?\n");
					fflush(stdin);
					gets(ficheiro);
					strcat(drive, ficheiro);
					f= fopen(drive, "wt");
					
					if (f == NULL)
						puts("Impossivel abrir ficheiro");
						exit(1);
					else 
						printf("Quantos numeros romanos quer converter?: ");
						scanf(" %d", &ne);

						Arabe = (int*) malloc (ne * sizeof(int));
						NRomano = (char**) malloc (ne * sizeof(char*));

						printf("\n\n");
						for (i=0; i<ne; i++) 
							NRomano[i] = (char*) malloc(ne * sizeof(char));
							
						
						for (i=0; i<ne; i++) {
							printf("Digite o numero romano: "); 
							fflush(stdin);
							gets(NRomano[i]);
							Arabe[i] = teclado(NRomano[i]);
						}
						printf("\n\n");
						for (i=0; i<ne; i++){
							fprintf(
					
						
						
		
				break;*/
			case '2':

					FILE *fp;
					int k, *Arabe, ne;
					char **NRomano;
	

						system("cls");		
						fp = fopen("C:\\romano.txt","w");
			
						printf("Quantos numeros romanos quer converter?: ");
						scanf("%d", &ne);
						
						NRomano = (char**) malloc (ne * sizeof(char*));
						Arabe = (int*) malloc (ne * sizeof(int));
						printf("\n\n");

						for (k=0; k<ne; k++) 
							NRomano[k] = (char*) malloc(ne * sizeof(char));

						for (k=0; k<ne; k++) {
							printf("Digite o numero romano: "); 
							fflush(stdin);
							gets(NRomano[k]);	
							Arabe[k] = teclado(NRomano[k]);
							
						}
						printf("\n\n");
						for (k=0; k<ne; k++){
							printf("O numero romano %s = %d em arabe\n", NRomano[k], Arabe[k]);		
							fprintf(fp,"O numero romano %s = %d em arabe\n", NRomano[k], Arabe[k]);
						}
				
				break;
			case '3': exit(1);
				break;
		}
		printf("\n");
		printf("Deseja Sair?\n");
		fflush(stdin);
		scanf("%c", &op);
	}while( op == 'n');
}

so the problem resides when start the program and enter "2" on the menu, i input how many numbers i want to convert lets says i input "1" the the program asks me to input a roman number i input "VII" and i get the output of that number in normal "7" number but when i get the output the assertion error appears and this problem appeared when i started inputing the file part of the program in the "case 2:" and what im aiming to acomplish is when the output is showned on the screen its also written in the file that i created. The function "teclado" converts the roman number and return a numerical number.
can someone help me?
  #6  
Old 15-Jun-2012, 18:03
Rsk08 Rsk08 is offline
New Member
 
Join Date: Jun 2012
Posts: 6
Rsk08 is on a distinguished road

Re: Assertion expression error (str != NULL)


Quote:
Originally Posted by davekw7x
Hmmm. I see the code. but it can't be compiled with my compiler:
Compiler messages (GNU gcc version 4.1.2 on my Centos 5.8 Linux workstation) are
Code:
gcc -Wall -W -pedantic --std=c99 zz.c -o zz zz.c:55: warning: return type of ‘main’ is not ‘int’ zz.c: In function ‘main’: zz.c:124: error: expected expression before ‘FILE’ zz.c:130: error: ‘fp’ undeclared (first use in this function) zz.c:130: error: (Each undeclared identifier is reported only once zz.c:130: error: for each function it appears in.)

I hate to repeat myself, but suggestion number 1 was to submit a program that we can compile and execute. I mean, you previously had a program that you could compile and execute, right?


I don't see any explanation or responses to my other suggestions.

Were any parts of my previous suggestions unclear?


Would you like to try one more time?


Regards,

Dave


sorry about that i sent it by mistake before writting the explanation
  #7  
Old 16-Jun-2012, 08:22
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,147
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Assertion expression error (str != NULL)


Quote:
Originally Posted by Rsk08
sorry about that i sent it by mistake before writting the explanation
Did you compile the C program code that you posted? It is not valid C code.

Are you sure that you are compiling it as a C program? (What is the name of the source code file?)

It will not compile as a C program with any of the various Borland, Microsoft and GNU compilers that I have at my disposal. I showed error messages from attempts to compile with a GNU compiler, I won't bore you with details of error messages from my other compilers. What I would like to know is what you are using and how you are compiling.

I did suggest that you tell us what compiler and operating system you are using, which you have not, so far, chosen to do. I should have also suggested that you tell us how you are compiling. (From command line, from Integrated Development Environment such as Visual Studio, or what?)

Here's the thing:
There is no way that I can see to help you learn to debug your C program unless we start on the same page.


I mean, regardless of C-language syntax problems there are some non-standard usages and some places where undefined behavior can result, but I don't see any point in my guessing...


Regards,

Dave
__________________
Sometimes I just can't help myself...
  #8  
Old 16-Jun-2012, 08:57
Rsk08 Rsk08 is offline
New Member
 
Join Date: Jun 2012
Posts: 6
Rsk08 is on a distinguished road

Re: Assertion expression error (str != NULL)


I compiled the program with visual studio 2010
  #9  
Old 17-Jun-2012, 00:00
Rsk08 Rsk08 is offline
New Member
 
Join Date: Jun 2012
Posts: 6
Rsk08 is on a distinguished road

Re: Assertion expression error (str != NULL)


I solved the problem and now i write into the file, sorry about my bad explanation of the problem, if i get some other error, and i dont understand i´ll try to explain it better, but thanks anyways.
 
 

Recent GIDBlogGID Spam Detector 1.1.0 by gidnetwork

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[MFC] How to enable scrollbars in DIBLOOK result window? szmitek MS Visual C++ / MFC Forum 0 19-Dec-2011 18:58
C++ Linker errors Rui Jorge Cossa C++ Forum 2 27-Oct-2011 23:51
join problem zuzupus MySQL / PHP Forum 0 14-Aug-2003 05:11
MySQL Syntax Error DropZite MySQL / PHP Forum 3 09-Jul-2003 04:00

Network Sites: GIDNetwork · GIDApp · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:38.


vBulletin, Copyright © 2000 - 2013, Jelsoft Enterprises Ltd.