# Need help with converting strings to integers!

Page 1 of 1

## 5 Replies - 2367 Views - Last Post: 19 November 2008 - 08:25 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=72527&amp;s=0ae50f66ad7fc03f1b64e62f1eafb356&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 andrewmickey15

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

# Need help with converting strings to integers!

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 **

Is This A Good Question/Topic? 0

## Replies To: Need help with converting strings to integers!

### #2 n8wxs

• --... ...-- -.. . -. ---.. .-- -..- ...

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

## Re: Need help with converting strings to integers!

Posted 18 November 2008 - 07:32 PM

```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

### #3 irishstevie

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

## Re: Need help with converting strings to integers!

Posted 19 November 2008 - 06:44 AM

andrewmickey15, 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 **

```#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.

### #4 red_4900

• Code T(h)inkers

Reputation: 21
• 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 " "

### #5 andrewmickey15

Reputation: 0
• 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.

### #6 irishstevie

Reputation: 0
• 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.