13 Replies - 1221 Views - Last Post: 21 January 2015 - 09:28 AM Rate Topic: -----

#1 sakhaout   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-January 15

I get error in if statement

Posted 20 January 2015 - 12:59 AM

#include<iostream>
#include<cstring>
using namespace std;
#define row  2
#define coll  2

void registration();
void expense();

int main()
{
	int choice;
	
	do
	{
		string info[row][coll];
		
		cout<<"Chose your options."<<endl<<endl;
		cout<<"1. Register"<<endl;
		cout<<"2. Enter Expense."<<endl;
		cout<<"3. View Report."<<endl;
		cout<<"4. Exit."<<endl<<endl;
		cout<<"Please enter your choice: ";
		cin>>choice;
		
		switch(choice)
		{
			case 1:
				registration();
			break;
						
			case 2:
				expense();
			break;
			
			case 3:
			break;
			
		}
	}
	while(choice!=4);	
}

void registration()
{
	string info[row][coll];
	for(int i=0;i<row;i++)
	{
		for(int j=0;j<coll;j++)
		{
			cout<<"Please enter name: ";
			cin>>info[i][j];
		}
		
		
	}
	

}

void expense()
{
	string name;
	double amount;
	string info[row][coll];
	cout<<"Please enter name: ";
	cin>>name;
	
	
			if(name==info[row][coll])
			{	
				cout<<"right "<<endl;
			}
			else
			{
				cout<<"Wrong"<< endl;
			}
		
		
		
	
	
	
}]


Is This A Good Question/Topic? 0
  • +

Replies To: I get error in if statement

#2 sakhaout   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-January 15

Re: I get error in if statement

Posted 20 January 2015 - 01:21 AM

I am new at programming. so I am trying to find out the easy to solve this problem.
please anyone can help me. please
Was This Post Helpful? 0
  • +
  • -

#3 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6968
  • View blog
  • Posts: 31,958
  • Joined: 10-May 07

Re: I get error in if statement

Posted 20 January 2015 - 05:24 AM

You have opened a topic indicating that you have an error in an if statement, posted code with no if statements that I can find, & then followed up with another post that you are new & need help.

Can you do me a favored & clarify what you are trying to do & what error you are getting? You must realize that this is not our project & we are not standing over your shoulder. The only information that we have is what you provide & I have no idea why the problem is.
Was This Post Helpful? 1
  • +
  • -

#4 sakhaout   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-January 15

Re: I get error in if statement

Posted 20 January 2015 - 04:05 PM

in void registration(); function get some members name from the user and that will be store in an array.
and again in void expense(); function get members name from the user. if this members are match to the void registration() function than this program print RIGHT otherwise print WRONG
Was This Post Helpful? 0
  • +
  • -

#5 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6968
  • View blog
  • Posts: 31,958
  • Joined: 10-May 07

Re: I get error in if statement

Posted 20 January 2015 - 04:11 PM

Please use code tags when posting code & avoid posting invalid tags otherwise. Not sure why your posts contain multiple end bold tags & are surrounded by empty tags.
Was This Post Helpful? 0
  • +
  • -

#6 vividexstance   User is offline

  • Tiocfaidh ár lá
  • member icon

Reputation: 794
  • View blog
  • Posts: 2,880
  • Joined: 31-December 10

Re: I get error in if statement

Posted 20 January 2015 - 04:11 PM

You need to be passing that info array around as a function argument, because each time you declare it inside a function, it's a new array that isn't initialized.
Was This Post Helpful? 1
  • +
  • -

#7 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6968
  • View blog
  • Posts: 31,958
  • Joined: 10-May 07

Re: I get error in if statement

Posted 20 January 2015 - 04:18 PM

View Postvividexstance, on 20 January 2015 - 06:11 PM, said:

You need to be passing that info array around as a function argument, because each time you declare it inside a function, it's a new array that isn't initialized.

Perhaps this is misunderstood because often new coders don't assign a value to their variables when they are declared. So it is assumed they still contain a value despite being local within the function.
Was This Post Helpful? 0
  • +
  • -

#8 sakhaout   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-January 15

Re: I get error in if statement

Posted 20 January 2015 - 04:26 PM

[How would i initialized the 'into[][]' ]
Was This Post Helpful? 0
  • +
  • -

#9 sakhaout   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-January 15

Re: I get error in if statement

Posted 20 January 2015 - 06:48 PM

[can you tall me how would i initialize string info[row][coll] in void expense() function. please][/b]
Was This Post Helpful? 0
  • +
  • -

#10 vividexstance   User is offline

  • Tiocfaidh ár lá
  • member icon

Reputation: 794
  • View blog
  • Posts: 2,880
  • Joined: 31-December 10

Re: I get error in if statement

Posted 21 January 2015 - 07:19 AM

View Postno2pencil, on 20 January 2015 - 06:18 PM, said:

View Postvividexstance, on 20 January 2015 - 06:11 PM, said:

You need to be passing that info array around as a function argument, because each time you declare it inside a function, it's a new array that isn't initialized.

Perhaps this is misunderstood because often new coders don't assign a value to their variables when they are declared. So it is assumed they still contain a value despite being local within the function.

The OP initializes an array inside of a function but doesn't return it. So instead of declaring the array inside the function that initializes it with user input, pass it along as an argument.

To the OP, do you know what function arguments are?
Was This Post Helpful? 1
  • +
  • -

#11 dragonriderjess   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 9
  • Joined: 19-January 15

Re: I get error in if statement

Posted 21 January 2015 - 07:34 AM

View Postsakhaout, on 20 January 2015 - 06:48 PM, said:

[can you tall me how would i initialize string info[row][coll] in void expense() function. please][/b]

Variables in the main function will stay in the main unless called through passing by reference or by variable.
Because you are using an array, it would require a special way of declaring.
Example: char jessi(char (&board)[10][10]);
jessi is the name of the function, and note the array name is in brackets. As far as I'm concerned, you only need to put a number in one of the brackets, but I'm using two.
When calling the function, you would simply write the name of the array in the brackets.
Example: jessi(board);
Was This Post Helpful? 0
  • +
  • -

#12 dragonriderjess   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 9
  • Joined: 19-January 15

Re: I get error in if statement

Posted 21 January 2015 - 07:42 AM

Also, I'm not certain on this, but I don't think you need to call the main while declaring "using namespace std".

View Postdragonriderjess, on 21 January 2015 - 07:40 AM, said:

Also, I'm not certain on this, but I don't think you need to call the main while declaring "using namespace std".

*By calling I mean you don't need to put "int" before it.

*By calling I mean you don't need to put "int" before it.
[/quote]
Was This Post Helpful? -1
  • +
  • -

#13 vividexstance   User is offline

  • Tiocfaidh ár lá
  • member icon

Reputation: 794
  • View blog
  • Posts: 2,880
  • Joined: 31-December 10

Re: I get error in if statement

Posted 21 January 2015 - 09:15 AM

You should always declare the main() function to return an integer. The return value indicates whether the program succeeded or failed.
Was This Post Helpful? 0
  • +
  • -

#14 vividexstance   User is offline

  • Tiocfaidh ár lá
  • member icon

Reputation: 794
  • View blog
  • Posts: 2,880
  • Joined: 31-December 10

Re: I get error in if statement

Posted 21 January 2015 - 09:28 AM

View Postdragonriderjess, on 21 January 2015 - 09:34 AM, said:

Because you are using an array, it would require a special way of declaring.
Example: char jessi(char (&board)[10][10]);

This is making the whole thing more complicated than it needs to be, and possibly more confusing.

When passing arrays around to functions, a pointer to the first item in the array is passed. So there is no need for references.

Here is a simple example of initializing an array inside a function, and then another function using that initialized array to print the contents of the array. This is a very simple example, but you should be able to learn from it. Also note, if you're passing a 1-dimensional array to a function, you don't need to put the array's size in the function's declaration. Only with more than 1-dimension do you need to specify the extra dimension's sizes.

#include <stdio.h>

#define SIZE 10

void init(int arr[][SIZE])
{
	int i, j;
	
	for(i = 0; i < SIZE; ++i)
	{
		for(j = 0; j < SIZE; ++j)
		{
			arr[i][j] = i + j;
		}
	}
}

void print(int arr[][SIZE])
{
	int i, j;
	
	for(i = 0; i < SIZE; ++i)
	{
		for(j = 0; j < SIZE; ++j)
		{
			printf("arr[%d][%d] = %d\n", i, j, arr[i][j]);
		}
	}
}

int main(void)
{
	int arr[SIZE][SIZE];
	
	init(arr);
	print(arr);
	
	return 0;
}


This post has been edited by vividexstance: 21 January 2015 - 09:29 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1