5 Replies - 1804 Views - Last Post: 19 November 2008 - 08:25 AM Rate Topic: -----

#1 andrewmickey15  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-November 08

Need help with converting strings to integers!

Post icon  Posted 18 November 2008 - 07:11 PM

Hi, I need help doing the following: Write a program that inputs four strings that represent integers, converts the strings to integers, sums the values and prints the total of the four values. The following is what I've tried, I know you need a string header and such but am not sure on how to CONVERT them.

Here's what I got:

/*excercise 8.7*/

#include <stdio.h>
#include <string.h>

int main (void)
{
	int num1;
	int num2;
	int num3;
	int num4;
	int sum;
	char s1[num1];
	char s2[num2];
	char s3[num3];
	char s4[num4];

	printf("Enter the values for each string:\n");

	scanf("%s,%s,%s,%s"&char s1,&char s2,&char s3,&char s4);

	num1= char s1[];
	num2= char s2[];
	num3= char s3[];
	num4= char s4[];

	sum= char s1[]+char s2[]+char s3[]+ char s4[]

	printf("%d",sum);

	return 0;
}



I've had numerous errors. Anyone give me any pointers?

** Edit ** :code:

Is This A Good Question/Topic? 0
  • +

Replies To: Need help with converting strings to integers!

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Need help with converting strings to integers!

Posted 18 November 2008 - 07:32 PM

Please post your code examples using - :code:

int num1;
int num2;
int num3;
int num4;
int sum;
char s1[num1];
char s2[num2];
char s3[num3];
char s4[num4];


The integers have no values, so the char arrays will not have any size!
Pick a default input line length, say 80 characters, and initialize the char arrays with it:
char s1[80];
char s2[80];
char s3[80];
char s4[80];


Once you have entered the string values, you need to convert the strings into integers. Copying
will not work.
num1= char s1[];
num2= char s2[];
num3= char s3[];
num4= char s4[];


You need to use a function like atoi(). Here's a reference http://www.cplusplus...tdlib/atoi.html

This post has been edited by n8wxs: 18 November 2008 - 07:44 PM

Was This Post Helpful? 0
  • +
  • -

#3 irishstevie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-November 08

Re: Need help with converting strings to integers!

Posted 19 November 2008 - 06:44 AM

View Postandrewmickey15, on 18 Nov, 2008 - 06:11 PM, said:

Hi, I need help doing the following: Write a program that inputs four strings that represent integers, converts the strings to integers, sums the values and prints the total of the four values. The following is what I've tried, I know you need a string header and such but am not sure on how to CONVERT them.

Here's what I got:

/*excercise 8.7*/

#include <stdio.h>
#include <string.h>

int main (void)
{
	int num1;
	int num2;
	int num3;
	int num4;
	int sum;
	char s1[num1];
	char s2[num2];
	char s3[num3];
	char s4[num4];

	printf("Enter the values for each string:\n");

	scanf("%s,%s,%s,%s"&char s1,&char s2,&char s3,&char s4);

	num1= char s1[];
	num2= char s2[];
	num3= char s3[];
	num4= char s4[];

	sum= char s1[]+char s2[]+char s3[]+ char s4[]

	printf("%d",sum);

	return 0;
}



I've had numerous errors. Anyone give me any pointers?

** Edit ** :code:




#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
	int sum = 0;
	string s1,s2,s3,s4;

	cout << "Please enter 4 different Strings" << endl;
	cin >> s1 >> s2 >> s3 >> s4; // Input strings

	cout << s1.size() << endl; // Prints out the Integer value for s1
	cout << s2.size() << endl; // Prints out the Integer value for s2
	cout << s3.size() << endl; // Prints out the Integer value for s3
	cout << s4.size() << endl; // Prints out the Integer value for s4

	// size() Returns a count of the number of characters in the string

	sum = s1.size() + s2.size() + s3.size() + s4.size(); // Adds all the values

	cout << sum << endl; // Prints out all the values that was added

	system("PAUSE");
	return EXIT_SUCCESS;
}



This is the way i would have done it since you said about using strings hope this helps.
Was This Post Helpful? 0
  • +
  • -

#4 red_4900  Icon User is offline

  • Code T(h)inkers
  • member icon

Reputation: 21
  • View blog
  • Posts: 1,120
  • Joined: 22-February 08

Re: Need help with converting strings to integers!

Posted 19 November 2008 - 07:16 AM

Syntax error on line 20.

scanf("%s",&variable)

You missed the , after " " ;)
Was This Post Helpful? 0
  • +
  • -

#5 andrewmickey15  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-November 08

Re: Need help with converting strings to integers!

Posted 19 November 2008 - 08:07 AM

This is in C not C++ Im not too familiar with the differences of the two.
Was This Post Helpful? 0
  • +
  • -

#6 irishstevie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-November 08

Re: Need help with converting strings to integers!

Posted 19 November 2008 - 08:25 AM

C++ uses string.h where as C does not it uses char*[] (character pointer array)

C++ just makes it easier using strings because you just say:
string myString = "This is my string";



its easier to do all types of things on that string as i showed you size() returns the size of the string.

Look at a tutotial online about strings.

Anymore questions ill help you best that i can.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1