why its not creating a string??

This is a discussion on why its not creating a string?? within the C Programming forums, part of the General Programming Boards category; Code: char* name="canceled"; whats wrong with this line ??...

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    why its not creating a string??

    Code:
    char* name="canceled";
    whats wrong with this line
    ??

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,797
    There is nothing wrong with it by itself, but it would be better to write:
    Code:
    const char* name="canceled";
    to reduce the chance of an attempt to modify a string literal.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    works for me
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  4. #4
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    in this code i have these errors
    the first error is for the line i am asking about
    1>------ Rebuild All started: Project: ex6, Configuration: Debug Win32 ------
    1>Deleting intermediate and output files for project 'ex6', configuration 'Debug|Win32'
    1>Compiling...
    1>ex6.c
    1>c:\documents and settings\lun\my documents\visual studio 2005\projects\ex6\ex6\ex6.c(25) : warning C4996: 'fopen' was declared deprecated
    1> c:\program files\microsoft visual studio 8\vc\include\stdio.h(234) : see declaration of 'fopen'
    1> Message: 'This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.'
    1>c:\documents and settings\lun\my documents\visual studio 2005\projects\ex6\ex6\ex6.c(26) : warning C4996: 'fopen' was declared deprecated
    1> c:\program files\microsoft visual studio 8\vc\include\stdio.h(234) : see declaration of 'fopen'
    1> Message: 'This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.'
    1>c:\documents and settings\lun\my documents\visual studio 2005\projects\ex6\ex6\ex6.c(27) : warning C4996: 'fopen' was declared deprecated
    1> c:\program files\microsoft visual studio 8\vc\include\stdio.h(234) : see declaration of 'fopen'
    1> Message: 'This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.'
    1>c:\documents and settings\lun\my documents\visual studio 2005\projects\ex6\ex6\ex6.c(31) : error C2143: syntax error : missing ';' before 'type'
    1>c:\documents and settings\lun\my documents\visual studio 2005\projects\ex6\ex6\ex6.c(33) : error C2065: 'arr' : undeclared identifier
    1>c:\documents and settings\lun\my documents\visual studio 2005\projects\ex6\ex6\ex6.c(33) : warning C4047: 'function' : 'FILE **' differs in levels of indirection from 'int'
    1>c:\documents and settings\lun\my documents\visual studio 2005\projects\ex6\ex6\ex6.c(33) : warning C4024: 'report' : different types for formal and actual parameter 2
    1>c:\documents and settings\lun\my documents\visual studio 2005\projects\ex6\ex6\ex6.c(33) : error C2065: 'name' : undeclared identifier
    1>c:\documents and settings\lun\my documents\visual studio 2005\projects\ex6\ex6\ex6.c(33) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'int'
    1>c:\documents and settings\lun\my documents\visual studio 2005\projects\ex6\ex6\ex6.c(33) : warning C4024: 'report' : different types for formal and actual parameter 3
    1>Build log was saved at "file://c:\Documents and Settings\lun\My Documents\Visual Studio 2005\Projects\ex6\ex6\Debug\BuildLog.htm"
    1>ex6 - 3 error(s), 7 warning(s)
    ========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========


    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
     
    
     
    typedef struct vote {
    	char name [30];
    	int id,type,candidate;
    } vote;
    void report(FILE* college[3], FILE* arr[10], char* canceled);
    // Fuction returns the minimal id
    int getMin(int eof[],vote [],int *);
    // Function checks a vote record correct
    int votecheck(vote ,vote [],int i);
    // Function reads the next record from "college" file
    int readFrom(FILE *,vote [],int);
    
    
    
    int main ()
    {
    FILE* f[10];
    FILE* c[3];
    c[0]=fopen("c:\\file1","r");
    c[1]=fopen("c:\\file2","r");
    c[3]=fopen("c:\\file3","r");
    fclose(c[0]);
    fclose(c[1]);
    fclose(c[2]);
    char* name="canceled";
    
    report(c, arr, name);
    	return 0;
    }
    void  report(FILE *college[3], FILE *arr[10], char* canceled){
    	FILE *cancel;
    	vote currtable[3], // array for three current "college" records
    		votable[3],    // array for checking the records with the same "id"
    		current;	   // struct for the current "college" record	
    	int i,j,idmin,index,flag,eof[3];
    	if(!(cancel=fopen("c:\\canceled","w"))) exit(1);
    	
    	for(i=0;i<3;i++)
    		eof[i]= readFrom(college[i],currtable,i);
    // main loop	
    	while(eof[0]!=EOF||eof[1]!=EOF||eof[2]!=EOF){
    		idmin=getMin(eof,currtable,&index);
    		for(i=flag=0;i<3;i++)
    			if(i!=index && eof[i]!=EOF && currtable[i].id==idmin)
    				flag=3;
    		if(flag){
    		fprintf(cancel,"%s%d%d\n",currtable[index].name,idmin,flag);
    			for(i=0;i<3;i++)
    				while(eof[i]!=EOF && currtable[i].id==idmin)
    				eof[i]=readFrom(college[i],currtable,i);
    			continue;
    		}
    //Filling struct table for the minimal id
    		votable[0]=currtable[index];
    		i=1;
    		while(eof[index]!=EOF && i<3){
    			eof[index]=fscanf(college[index],"%30s%9d%1d%1d%*c",
    current.name,&current.id,&current.type,&current.candidate);
    		if(current.id!=idmin){
    			for(j=0;j<i;j++)
    				fprintf(arr[votable[j].candidate],"%9d%d%d\n",
    idmin,index,votable[j].type);
    		currtable[index]=current;
    		break;
    		}
    		else{
    			flag=votecheck(current,votable,i);
    			if(flag){				fprintf(cancel,"%s%d%d\n",currtable[index].name,idmin,flag);
    			while(eof[index]!=EOF && currtable[index].id==idmin)
    			eof[index]=readFrom(college[index],currtable,index);
    			break;
    			}
    			else
    				votable[i++]=current;
    		}//else
    	}//while
    	if(i<3)  continue;
    		if(eof[index]!=EOF)
    		eof[index]=fscanf(college[index],"%30[^\n]%9d%1d%1d%*c",
    current.name,&current.id,&current.type,&current.candidate);
    		if(eof[index]==EOF ||current.id!=idmin ){
    			for(j=0;j<i;j++)
    				fprintf(arr[votable[j].candidate],"%9d%d%d\n",
    idmin,index,votable[j].type);		
    			currtable[index]=current;
    		}
    		else{			fprintf(cancel,"%s%d%d\n",votable[0].name,idmin,2);
    	while(eof[index]!=EOF && currtable[index].id==idmin)
    		eof[index]=readFrom(college[index],currtable,index);
    		}
    	}// main loop - while
    	fclose(cancel);
    }// report
    
    int votecheck(vote curr,vote arr[],int i){
    	int j,count;
    	for(j=count=0;j<i;j++){
    		if(curr.candidate==arr[j].candidate)
    			return 1;
    		if(curr.type==1 && arr[j].type==1)
    			return 2;
    		if(curr.type==2 && arr[j].type==2)
    			count++;
    		if(count>1)	return 2;
    	}
    	return 0;
    }
    
    int readFrom(FILE *coll,vote table[],int i){
    	return fscanf(coll,"%30[^\n]%9d%1d%1d%*c",table[i].name,
    				&table[i].id,&table[i].type,&table[i].candidate);
    }	
    
    int getMin(int eof[],vote table[],int *index){
    	int i,idmin;
    	for(i=0,idmin=999999999;i<3;i++)
    		if(eof[i]!=EOF && table[i].id<idmin){
    				idmin=table[i].id;
    				*index=i;
    		}
    	return idmin;			
    }
    Last edited by transgalactic2; 04-11-2009 at 01:14 PM.

  5. #5
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    solved it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  2. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21