WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

How to write a tic tac toe game program in c language using int char array with the help of for & while loop

Using your knowledge of the C Language and a C compiler. Why do you ask? Oh, forgive me. You didn't ask. You simply made a statement. Nevermind.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

You could try something like below

thisNumber += atoi((char*)&i);/*This only works for Intel/AMD type integers*/

atoi() only works on c-strings, not on characters. ZedChu's original code was better: i = (i - 48); , though his loop is incorrect.

while ((i = getchar()) != '\n') 
{
    thisNumber *= 10;      // this is good
    i = (i - 48);          // this is OK.  Instead of 48, use '0'
    thisNumber += (i*10);  // why are you multiplying the digit by 10?
}
thisNumber /= 10;          // why are you dividing the value by 10?

If you aren't sure, desk check your loop to see if it's processing your input properly.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

I am not getting all numbers but only last number while readind data from file.
And it has to be done with fscanf function.

See the comments:

/*  input n nunbers fom user and write it in the file.find it's sum
	 and  write the ans. in another file                   */
#include <stdio.h>
#include <conio.h>        // do not use this header, it's not portable.
void main()               // main is an INT function, not VOID
{
	int i,n,no,sum=0;
	FILE *fp,*fs;
	fp=fopen("d:\\tc\\bin\\strtNo.txt","w");
	printf("%s","Enter value for n : ");
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
		printf("\n Enter the no : ");
		scanf("%d",&no);
		fprintf(fp,"%d\n",no);
	 	sum+=no;
	}
	fprintf(fp,"%d\n",99); 
	fclose(fp);
	fs=fopen("d:\\tc\\bin\\strtSum.txt","w");
	fprintf(fs,"%d",sum);
	fclose(fs);
	fp=fopen("d:\\tc\\bin\\strtNo.txt","r");
	if(fp==NULL)
	{
		printf("ERROR");
	}
	while((fscanf(fp,"%d\n",no))!=99)  // What is the return value of fscanf?
	{                                  // You need to look it up...
		printf("%d\n",no);
	}
	fclose(fp);
	getch();           // Don't use -- it's not portable.  But GETCHAR() is!
}
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

You have to copy the characters you want into the new arrays. In your example you will have to declare four character arrays (not pointers!) that are 3 characters each, the 3d one is for the null terminating character.

No, only 2 characters each. He wants character arrays, not c-strings.

There is no reason to allocate space for a trailing null if you are dealing with arrays, only with strings.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

the output of my program is the number of letters in the text file..but if it read a big letter 'A' and small letter 'a' the program should read it as 1,that big letter A and a are the same

So add the number of 'A' and 'a' together before you print out the value.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

is my print still in the wrong loop?
cause, it still doesn't seem to be printing any results at all, and i'm not sure I'm following the loop correctly...

Come on!!! Format your code properly! Still can't read it.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

You sure do. Try asking a lucid question in your post explaining exactly what you want to do. Your title is nonsensical.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Because 3.3 is not 3.3 in computers. Floating point is inaccurate.

3.3 could really be 3.300001 or 3.299998.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Create a desktop icon for the program and move it into your Start:Programs:StartUp folder. At least that's where they go in XP

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

I have a problem with this code.

Here's what I see as your major problems:
Lack of proper formatting makes the code difficult to read #include<conio.h> -- using non-standard header. It's not portable and (as you can see from VernonDozier) you can't get adequate help. #include "gotoxy.h" -- Same as conio.h fflush(stdin); -- See this system("cls"); -- Calling the system is not a good idea. Clearing the screen is annoying to the user at best. And it's not portable -- not all O/S's have the command CLS. system("color 0E"); -- Same as system("cls"); getch(); -- non-standard and not portable. getchar() is standard and portable. gets(n); -- Dangerous. See this fprintf(fp," "); -- Why call such a heavy, complicated function to output one space? How about fputc()? gotoxy(35,13); -- non-standard and not portable. goto start; -- C++ is designed to not need goto's which helps make poorly designed code. Use loops and other constructs.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Ok the thing I searched into google brought up a ton of nonsense.

Lack of understnding does not make the information nonsense...

I want to use variables from a function in main...

Pass the values back to main() when the function returns.

... and (more important) variables from main in a function.

Pass the values into the function in the parameter list.

Both of these can easily be found in any tutorial or book on C/C++.

PS How do I combine strings?

Depends on the type of 'string' you use. Either a simple + if you're using strings or strcat() if using char*

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Then you did it wrong. But you didn't bother to show us what you did, so we can't be more specific.

And u is not an English word. Please don't use leet speek on the forums.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

A somewhat minor nit-pick:

In ASCII the letters 'a' to 'z' and 'A' to 'Z' are converted a continuous set of numbers when you convert them from char to int .

Letters are not converted to numbers, they are numbers. 66 is a number, and if output as a character value, it simply gets displayed as 'B'. If output as an integer, it gets displayed as 66.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Hi ,

can you plz explain it with code :)

thanks for your help

No. We to not write homework programs for people. That's your job as the student. We can help you when you get stuck, but based on your posts, you aren't stuck since there's no attempt to solve the problem.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

can you please give me the algorithm to convert English into Latin .I want an example like if there is a word "test" than what would be it in Latin.

1) Buy a Latin-English Dictionary.
2) Look up English word in the dictionary.
3) Read the Latin translation given.
4) Use the Latin word 10 times that day and the word will be yours to use.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Why are you printing in the middle of the sort? Don't you need to sort then print the sorted list?

I'm sorry its so horribly written:

Then read this and don't write horrible code anymore :icon_wink:

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

1) Don't create variables with _ as the first character. That's used for internal values
2) top, bottom, whole are probably integers.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Hi, I'm wondering how to remove a specific part of a string..

Thank you, I am still learning, but also getting the hang of strings as I work this more!

Heres the thing, I don't want to remove part of the string with values (example +1, -7). Isn't there a way I can find the comma (,) within the string and remove it and whats before it?

Yes there is. Read a little more about strings.

And how would this be done?

I would recommend rereading the stuff you are "getting the hang" of and start attempting to solve the problem. You also need to stop telling us you have no intention of doing it yourself and you want us to do it for you. If that's not what you meant, sorry, but that is exactly what your second and third post is telling us.

The Member Rules state you need to post an attempt to get help. The code you posted attempted nothing.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Once you get the data from argc and argv, stop using the values. All your data is in your variables now. Just use the variables.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

The compiler. Press/click the build or run button and if your syntax is wrong, the compiler will tell you.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

When will noobies stop posting questions without the information they have that can easily answer their question?

When posting errors, don't post a part of the error -- copy and paste the ENTIRE error, verbatim, including line number, listed code, EVERYTHING.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

So where are you now? Remember, we can't see your screen. And we can't read your mind...

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

1) Is argv[x] an integer? I'm assuming it's an integer, yes. Why wouldn't it be? It's the number of values in the array minus one, correct?

you can't assume. You have to know. See where assuming got you? :icon_wink: int main(int argc, char *argv[]) Look carefully. What is argv[x]?

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Then something else is wrong.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Can someone help me in building a syntax for this problem

Write a program that accepts a positive integer and gives its prime factorization that expresses the integer as a product of primes.

Here's one that I did:

#include<stdio.h>
#include<conio.h>    // remove, not needed
void main()          // main is NEVER void, it's int
int N,R;             // why aren't you indenting so we can follow this?
clrscr();            // Get rid of this annoying line
printf("Please enter a no.:\n");
scanf("%d",&N);
{ for(R==2;R>N;R++)
 if(N%R==0)
 printf("%d",R)
 else if(N%R!=0)
 printf("%d",N);
 else 
 printf("Invalid");
}
getch();             // Get rid of this, it's not standard. 
                     // Use something standard like getchar()
}

Other than that, what do you need help with? You need to ask a question that can be answered. "Can someone help?" doesn't give us any useful info to describe what you need help with.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

You obviously didn't try everything. Including reading the Member Rules.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Hello fellow members of daniweb

Hi

I've been rattling my brain for some time now on how to use a number code of sorts

I've never played with "a number code of sorts". How do they work? Are they different from numbers? How?

to output the name of a person, sort of like a id system.

Match the "number code of sorts" with people's names. When you get one, output the other.

I'd really appreciate if someone or anyone could help me with this predicament.

If this doesn't help, maybe a couple (or preferably more) details would help since you know more about the project than we do.

Thank you in advance.

Welcome... maybe.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Hi,

This is my first post here at DaniWeb. I've been using it as a resource through out my assignments. I am truly eager to learn C++, but I've gotten stuck.

All I am trying to do is average an array of numbers, but I haven't been able to even sum date correctly yet, so I'm taking baby steps.

Here's what I have:

#include <iostream>
#include  <stdlib.h>

int sum(int x[], int dim)
{     int ans=0;
      for (int i=0; i< dim; i++)
          ans += x[i];
      return ans;
}

int main(int argc, char *argv[])
{ 	int x = argc-1;
	int a = argv[x];
	int arr[1000];
    std::cout << "total = " << sum(arr, a) << std::endl;
    return 0;
}

and I'm not even sure if it works because I am receiving this error, which I thought I could fix with "atoi"

error: invalid conversion from ‘char*’ to ‘int’

Any help is greatly appreciated. Thanks

Well, since you didn't bother to tell us where the error is we can only guess. It is always helpful to give us all the information you can. Don't leave things out. You never know what would be important.

My guess is you have a problem with the line int a = argv[x]; Questions to answer:
1) Is argv[x] an integer?
2) What type of variable is it?
3) Now, what does the error say?
4) Got it now?

Hope so...

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

You only gave us half the information. The starting file data. It helps to give us the ending file data so we know the target we're shooting at.

You also didn't tell us exactly what the "little help" you needed was. Just that you needed help. Explain in detail your problem.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

scanf("%c",&sign);
scanf("%c",answer);

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

And more terrible code... Please stop posting just to boost your ego.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

And is this abominable code useful in some way? Other than to show how not to code?

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

See this series of posts.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

right, I remember doing stuff like that in class with the remainder. but I can't really remember how to code that in terms of
weeksworked = hoursworked / 40;

Then you'd better start reading your book and asking questions in class. We are not a tutorial forum. Nor are we a homework service forum.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Yeah, can you finish his homework for him? He has to pass the class, you know.

Süspeñce commented: no need to tell me ..i know better -1
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Comare your input for sign and answer. Do you see anything missing?

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Does it work?

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Output the numbers to the printer
Cut the numbers from the paper
Sort them on the table
Tape them together when sorted.
:icon_twisted:

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

I would:
1) ask the user to insert the length of two sides (b and c) of a tringle
2) and the angle between them in degree (x)
3) compute and print the length of the third side,a, by using this formula

a^2=b^2+c^2-2bc(cos(x))

Please Note: the angle must be in radians instead of degrees so multiply the angle by pi/180

There you go. Anything else?

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

... but I have been on here since August 23rd, or somewhere in there, and I already have 135+ posts. I have been contributing to the site as much as possible and if the post count determines the knowledge or the credibility of a user...

It does not. I've seen members rack up more posts in 1 week than you have in 2 months. 99% of their posts were crap, error filled, and just bad advice.

Not saying your posts are bad, just saying post count means nothing specific -- other than pointing out who has a life and who doesn't :icon_wink:

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

You aren't testing Board[Row][Col-1] nor Board[Row][Col+1] .
Wouldn't two nested loops be better than all those IF statements?.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Details.

If you take your car to the mechanic, you can't just tell him "it doesn't work". You have to give him details.

Same with programming. If you want help, you need to explain
1) what's going wrong
2) how you know it's wrong
3) best guess as to where the code is wrong
4) what it should have done

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

What does "my code for the maze generator is not working"? Are we supposed to guess?

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

As L7sqr implied:

int myatoi( char *x ) {

    int y = ( *x - 48 );  // convert the first character into an integer
                          // What about the second, third,... characters?
    return y;
}

Think loop...

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Yes. Start by rereading my previous post -- first line..

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

>>But instead of showing A, 2, ..., J, Q, K, it shows 1 to 13.
How can I convert 1, 11, 12, 13 back to A, J, Q, K?

You'll have to use conditionals (i.e. an "if" statement) and alter the output based on the value.

I would use an array instead.
Now it's showing 1 to 13. Set up an array string cardVal[] = "A", "1", "2",... "10", "J", "Q", "K"}; and use the value that is currently being displayed as an index into the above array and print the string instead.

Fbody commented: Not a bad idea. :) +13
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

How is it impossible if the compiler has it. It is obvious that it can be written in Assembly. Well in that case I am in the wrong forum. Thanks!

If other forums explain how this can be done, please come back and explain it to us and we will apologize profusely. We will also learn something even we don't know.

NathanOliver commented: I guesss he does'nt get it. +9
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Wait I can really do that!?!

No. I only said that because you won't accept that a portable CLS cannot be done.
Honest, it cannot be done.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Oh, why can't I just extract the code widows runs when cls is called and put that in my program?

Go for it...